Re: Help with 3 compiler warnings from g++
* Louise Hoffman:
Dear developers,
I have written this program which can be downloaded from
http://www.sendspace.com/file/3uqpq1
as a 8Kb zip file, but I get these 3 warnings
main.cpp:101: warning: comparison between signed and unsigned integer
expressions
Expr.cpp: In member function 'virtual std::string Expr::toString()':
Expr.cpp:14: warning: control reaches end of non-void function
Expr.cpp: In member function 'virtual double Expr::eval()':
Expr.cpp:12: warning: control reaches end of non-void function
I have no idea why I get the first one. The code in question is
for( int i = 0; i < expressions.size(); i++ ) {
cout << expressions[i]->toString() << " = " << expressions[i]-
eval
() << endl;
}
Presumably expressions.size() has unsigned result type such as size_t.
Comparing a value of signed type to a value of unsigned type is generally
unsafe, e.g. -1 > 0u will yield true due to the -1 being promoted to unsigned type.
One solution is to define e.g.
#include <standard_header_that_defines_ptrdiff_t>
typedef std::ptrdiff_t Size;
typedef Size Index;
template< typename T >
Size nElements( T const& collection ) { return Size( collection.size() ); }
and then write
for( Index i = 0; i < nElements( expressions ); ++i )
{
// ...
}
and for the other two warnings it is
double Expr::eval() {}
string Expr::toString() {}
Missing return.
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!