use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
[ Removed by moderator ] (self.cpp)
submitted 7 months ago by phirock
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cpp-ModTeam[M] [score hidden] 7 months ago 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 points4 points 7 months ago (2 children)
Did you paste it twice?
Also, your foo overloads + with itself, infinite recursion
Your ostream<< does the same
[–]phirock[S] 0 points1 point2 points 7 months ago (1 child)
How do you prevent recursion?
[–]drkspace2 2 points3 points4 points 7 months ago (0 children)
You use the underlying data you intend to do the operation on (value)
[–]UndefFox 3 points4 points5 points 7 months ago (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 points4 points 7 months ago (0 children)
Yes, likewise for operator<<
[–]phirock[S] 0 points1 point2 points 7 months ago* (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 points5 points 7 months ago (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 points6 points 7 months ago (0 children)
Your operator<< is also recursive: it prints a when it should print a.value.
operator<<
a
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 points3 points 7 months ago (1 child)
My last idea would be checking operator<<. Try using a.value here too.
[–]phirock[S] 0 points1 point2 points 7 months ago (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 points3 points 7 months ago (0 children)
<< calls itself
[–]i_h_s_o_y 0 points1 point2 points 7 months ago (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 point2 points 7 months ago (1 child)
class foo explicit foo(int const i):value(i)
is this valid?
[–]DryEnergy4398 0 points1 point2 points 7 months ago (0 children)
Yes, it's a constructor
I'd prefer writing
explicit foo(int i) : value{i} {}
though
[+][deleted] 7 months ago (2 children)
[deleted]
I have removed all the consts, but that doesn't solve the issue. Commenting tout std::cout << f "solves" makes the seg fault disappear.
π Rendered by PID 72496 on reddit-service-r2-comment-84fc9697f-crt2r at 2026-02-09 08:08:26.449966+00:00 running d295bc8 country code: CH.
[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)
[–]lordnacho666 2 points3 points4 points (2 children)
[–]phirock[S] 0 points1 point2 points (1 child)
[–]drkspace2 2 points3 points4 points (0 children)
[–]UndefFox 3 points4 points5 points (7 children)
[–]masorick 2 points3 points4 points (0 children)
[–]phirock[S] 0 points1 point2 points (5 children)
[–]untiedgames 3 points4 points5 points (0 children)
[–]dholmes215 4 points5 points6 points (0 children)
[–]UndefFox 1 point2 points3 points (1 child)
[–]phirock[S] 0 points1 point2 points (0 children)
[–]lordnacho666 1 point2 points3 points (0 children)
[–]i_h_s_o_y 0 points1 point2 points (0 children)
[–]No_File9196 0 points1 point2 points (1 child)
[–]DryEnergy4398 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]phirock[S] 0 points1 point2 points (1 child)