Public Group

# Simple C++ question

This topic is 3547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay so I'm working on C++ again and going through a new book and am told this simple code should error due to missing less-than operator but doesn't(at least not MS VS2008 which I'm using):
#include <iostream>
using namespace std;

int main()
{
cout < "Hello, world!";
return 0;
}


Can anyone with a different compiler run it and tell me if it compiles without error? I don't have Linux installed anywhere at the moment unfortunately:( And if there are any C++ guru's out there can you tell me why this still compiles and executes since I figured it wouldn't due to missing "<" Thanks

##### Share on other sites
test.cpp: In function ‘int main()’:test.cpp:6: error: no match for ‘operator<’ in ‘std::cout < "Hello, world!"’test.cpp:6: note: candidates are: operator<(const char*, const char*) <built-in>test.cpp:6: note:                 operator<(void*, void*) <built-in>

##### Share on other sites
It doesn't error out for me but does give me this warning:

warning C4552: '<' : operator has no effect; expected operator with side-effect

It does run, but displays nothing. I'm using MS VS 2008 as well...weird quirk?

EDIT: Though using GNU GCC produces same errors that pinacolada posted.

[Edited by - Kilom on January 5, 2009 10:37:45 PM]

##### Share on other sites
Here's my guess:

"Hello World!" is of type const char[13]. cout is an object of type ostream. Since arrays can be decayed into pointers implicitly, "Hello World" can become a const char*, which can also match any argument taking a void*. ostream has an operator void*, which from the pinacolada's post, would be a matching function call for operator<(void*, void*).

Again, I haven't checked it. But my guess is it implicitly converts ostream into a void* in order for it to work. Whether the standard allows it or not, I'm not certain, but it's C++, so if you guess "undefined" you'll be correct most of the time. Again, it's just a quick guess.

You should be able to walk through the debugger and see which function is being called. Then it's only a question whether what it's doing is allowed by the standard.

EDIT: Ok, I just checked, my theory is correct. "C4552: '<' : operator has no effect; expected operator with side-effect", is happening since you're comparing two values and ignoring the result. However, walk through with the debugger and you'll see operator void*() being called.

##### Share on other sites
Quote:
 Original post by KilomIt doesn't error out for me but does give me this warning:warning C4552: '<' : operator has no effect; expected operator with side-effectIt does run, but displays nothing. I'm using MS VS 2008 as well...weird quirk?

Yeah I did get that warning too.
Okay I did some more testing and it seems that using a char or number literal will cause an error. Only a string literal seems to get past the compiler *weird* to say the least but it must be allowed by the standard since I assume automated testing would catch such a simple bug. Well MS VC considers it valid code while GCC does not that much I now know.

p.s. Okay I finally busted out my old apple powerbook to run this through GCC and it doesn't like this code and fails to compile and does spit out an error like I would've if I was the compiler-LOL!
I guess pinacola must've been running a Mac or Linux machine since the error he listed previously is the same exact error gcc spits out.
Well hand over an extra point for GCC since it's error message was alot more informative in this case!

[Edited by - daviangel on January 5, 2009 11:48:28 PM]

##### Share on other sites
Quote:
 Original post by gekkoHere's my guess:"Hello World!" is of type const char[13]. cout is an object of type ostream. Since arrays can be decayed into pointers implicitly, "Hello World" can become a const char*, which can also match any argument taking a void*....

As said by Gekko -> Arrays can be decayed into pointers. But seeing as you're probably near the beginning of the book, you'll learn about pointers later on...

##### Share on other sites
It's not actually a bug, gekko's sussed it [smile], I can only assume that the book is quite old or the author didn't actually bother to thoroughly test their code first.

As an aside, the simplest hello world program in C++ is just this:

#include <iostream>int main(){    std::cout << "Hello World!";}

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 18
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007477
• ### Who's Online (See full list)

There are no registered users currently online

×