Strange C++ Problem

Started by
14 comments, last by iMalc 12 years, 4 months ago
Is there any reason you haven't posted an actual, compilable example so we can try this out?
Advertisement
You're more than welcome.

Source

Compiles with g++ 4.4.5.

Links with libgmp.a, libboost_thread.a and posix threads.

Thanks.
Well - I made a quick port to VS2010 and found the problem.

I assume there's a reason the gnu compiler allows this, even with -Wall...


big_integer& big_integer::operator-=(const big_integer& rhs)
{
mpz_sub(_value, _value, rhs._value);
}


The method is not returning anything. I'm amazed it compiles, but I feel like a bit of an idiot to be honest.

Thanks very much for your help.
Hmm...

$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ *.cpp -lboost_thread -lgmp -Wall -Werror && ./a.out
big_integer.cpp: In member function ‘big_integer& big_integer::operator+=(const big_integer&)’:
big_integer.cpp:46:1: error: no return statement in function returning non-void [-Werror=return-type]
big_integer.cpp: In member function ‘big_integer& big_integer::operator-=(const big_integer&)’:
big_integer.cpp:51:1: error: no return statement in function returning non-void [-Werror=return-type]
big_integer.cpp: In member function ‘big_integer& big_integer::operator*=(const big_integer&)’:
big_integer.cpp:56:1: error: no return statement in function returning non-void [-Werror=return-type]
big_integer.cpp: In member function ‘big_integer& big_integer::operator/=(const big_integer&)’:
big_integer.cpp:61:1: error: no return statement in function returning non-void [-Werror=return-type]
big_integer.cpp: In member function ‘big_integer& big_integer::operator%=(const big_integer&)’:
big_integer.cpp:66:1: error: no return statement in function returning non-void [-Werror=return-type]
cc1plus: all warnings being treated as errors
[/quote]
Indeed... I guess it's the older version of g++ that is provided with oracle linux.
Telios, what happens if you change the return type of your operator- to big_integer&?

This solution seems counter-intuitive. Perhaps you should try putting the subtraction logic in the subtraction operator and having the subtraction-assignment operator use the subtraction operator then assign the result. That would make this easier to debug and would make your logic easier to follow.


const big_integer big_integer::operator-(const big_integer& rhs) const
{
big_integer result;

// Subtraction logic

return result;
}

big_integer& big_integer::operator-=(const big_integer& rhs)
{
*this = *this-rhs;

return *this;
}

No! That is exactly the wrong thing to do, as anyone who knows much about operator overloading will tell you. Just look at previous forum responses by some of the most talented people on this forum and you'll see that 99.9% of the advice is to do it the way that it was.

Seriously, change it back again.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement