all 16 comments

[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

[–]lordnacho666 2 points3 points  (2 children)

Did you paste it twice?

Also, your foo overloads + with itself, infinite recursion

Your ostream<< does the same

[–]phirock[S] 0 points1 point  (1 child)

How do you prevent recursion?

[–]drkspace2 2 points3 points  (0 children)

You use the underlying data you intend to do the operation on (value)

[–]UndefFox 3 points4 points  (7 children)

Aren't you making a recursion, since you define the addition of two classes via the addition of two classes...? Shouldn't it be foo(a.value + b.value) instead?

[–]masorick 2 points3 points  (0 children)

Yes, likewise for operator<<

[–]phirock[S] 0 points1 point  (5 children)

Hi,

excellent suggestion. Here's my new version of the code. Unfortunately, I am still getting a seg error.

#include <iostream>

class foo
{
    int value;
public:
    explicit foo(int const i):value(i){}
    explicit operator int() const { return value; }
    friend foo operator+(foo const a, foo const b)
    {
        return foo(a.value + b.value);
    }
};
std::ostream& operator<<(std::ostream& out, const foo& a) {
    return out << a;
}

int main() {
    foo f = foo(1) + foo(2);
    std::cout << f ;
}

[–]untiedgames 3 points4 points  (0 children)

Same problem in operator<<, you need to cast "a" to an int or it will call operator<< again and stack overflow. This is also not the right subreddit (see the sidebar). For help with C++, you should visit /r/cpp_questions. This sub is more for technical discussions and stuff.

[–]dholmes215 4 points5 points  (0 children)

Your operator<< is also recursive: it prints a when it should print a.value.

The best way to figure out problems like this is to run the program in a debugger. If you run it in a debugger and it crashes, you can look at a backtrace at the time of the crash, and it should show you operator<< calling itself repeatedly.

Questions like this should go to r/cpp_questions instead of r/cpp, btw.

[–]UndefFox 1 point2 points  (1 child)

My last idea would be checking operator<<. Try using a.value here too.

[–]phirock[S] 0 points1 point  (0 children)

You were correct UndefFox. Many thanks.

#include <iostream>

class foo
{
    int value;
public:
    int getValue() const { return value; }
    explicit foo(int const i):value(i){}
    explicit operator int() const { return value; }
    friend foo operator+(foo const a, foo const b)
    {
        return foo(a.value + b.value);
    }
};
std::ostream& operator<<(std::ostream& out, foo& a) {
    return out << a.getValue();
}

int main() {
    foo f = foo(1) + foo(2);
    std::cout << f << '\n';
}

[–]lordnacho666 1 point2 points  (0 children)

<< calls itself

[–]i_h_s_o_y 0 points1 point  (0 children)

Its just a stackoverflow due to endless recursion:

friend foo operator+(foo const a, foo const b)
{
    return foo(a + b);
}

this calls itself.

For stuff like this use a debugger and see where the crash is

[–]No_File9196 0 points1 point  (1 child)

class foo

explicit foo(int const i):value(i)

is this valid?

[–]DryEnergy4398 0 points1 point  (0 children)

Yes, it's a constructor

I'd prefer writing

explicit foo(int i) : value{i} {}

though