Jump to content
  • Advertisement
Sign in to follow this  
daviangel

Simple C++ question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Kilom
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?

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 this post


Link to post
Share on other sites
Quote:
Original post by gekko
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*....


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 this post


Link to post
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!";
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!