I'm currently looking at producing a C++ library. I've not much experience with C++ and have what is probably a very basic question about class instance method calling.
main.cpp
msgserver m;
std::thread t1(m.startServer, "192.168.50.128", 8081);
msgserver.h
class msgserver
{
public:
msgserver() { }
int startServer(std::string addr, int port);
};
msgserver.cpp
int msgserver::startServer(string addr, int port)
This code results in:
[C3867] 'msgserver::startServer': non-standard syntax; use '&' to create a pointer to member
I know i can fix the compiler error by making this method static but I'm unsure if that is a requirement imposed by the fact it's being called in a thread constructor (which doesn't allow the parens on the call signature) or if I need to figure out the syntax.
I've read around this and it's actually left me a bit more confused that when I started. It seems any fix I apply like:
int &msgserver::startServer(string addr, int port)
or
std::thread t1(&m.startServer, "192.168.50.128", 8081);
Is actually illegal syntax according to the compiler.
How can I call this as an instance method? Or is actually a good idea to start a thread running in the background on a static function?
std::thread t1(&msgserver::startServer, m, "<ip>", 8081);int y = 10; string x = "" + y;, which looks ordinary to a Java programmer, but is totally wrong in C++ (concatenating).intonto a string in C++, and they write that code. Then they can't understand why their C++ code doesn't work. But a C++ programmer looking at that same code would see it looks totally alien, weird, and just plain nuts. That is the perfect example of using another language as a model in writing C++ code, and totally failing.