For the fun and experience of it, I'm modifying and exploring the source code for Blobby Volley 2 1.0 (Linux).
Well... I would be modifying the source code, but I can't even get the program to compile. (Sad, isn't it?)
Here's the code that causes the error:
std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) {
return stream << val.name << " (" << val.hostname << ":" << val.port << ")";
}
Trying to compile this with g++ 5.4.0 gives the following (simplified output--the original output is ~443 lines) error message:
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘const char [32]’)
return stream << val.name << " (" << val.hostname << ":" << val.port << ")";
I simplified the code to this:
std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) {
stream << "hello"; //can't get simpler than this, right?
return stream;
}
and got
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘const char [6]’)
stream << "hello";
The code that calls it looks like this:
std::cout << "duplicate server entry\n";
std::cout << info << "\n"; //it's called here
The thing I find most surprising is that we all know that std::cout
and its ilk can handle char
arrays.
For instance,
#include <iostream>
#include <string>
int main () {
const char a[6] = "hello";
std::cout << a << std::endl; //No problem here!
return 0;
}
works without a hitch.
Oh, one more thing.
If I include <string>
, this works:
std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) {
stream << std::string("hello");
return stream;
}
Does anyone know what I'm missing?
PS: Here's a pastebin of the errors.
PPS: Here's the headers that were requested:
/* header include */
#include "NetworkMessage.h"
/* includes */
#include <cstring>
#include "UserConfig.h"
#include "SpeedController.h"
PPS: If you are wondering why I didn't get an error about std::ostream
not being defined, check the 3rd paragraph of Sam's answer.
#include <iostream>
- the only overload it knows about is the custom one you wrote. – Wallpaperiostream
was included in 'main.cpp', I'm still not sure why it was necessary, though.) – Freehanded