-1

I am trying to use a server interface to handle multiple servers depending on which one is available. Simplified, the code is like this:

Server.h

class Server : public ServerInterface {
public:
    Server();
    static Server * getServerPtr();
}

Server.cpp

#include "Server.h"

Server::Server () {}

Server * Server::getServerPtr() {
    Server * server = new Server();
    return server;
}

HMDServer.h

class HMDServer : public ServerInterface {
public:
    HMDServer();

    static HMDServer * getHMDServerPtr();

    void init();

private:
    int server_type;

HMDServer.cpp

#include "HMDServer.h"

HMDServer::HMDServer() {
    server_type = 1;
    init();
}

HMDServer * HMDServer::returnHMDServerPtr() {
    HMDServer * server = new HMDServer();
    return server;
}

void HMDServer::init() {
    //try to initialize server
    server_type = 111; // if initialization successful
}

ServerInterface.h

class ServerInterface {
public:
    static ServerInterface * getServer();
}

ServerInterface.cpp

#include "ServerInterface.h"
#include "HMDServer.cpp"
#include "Server.cpp"

ServerInterface * ServerInterface::getServer() {
    ServerInterface * server1 = HMDServer::getHMDServerPtr(); //This works perfectly
    if(server1->getServerType() == 1) {
        ServerInterface * server2 = Server::getServerPtr();
        return server2;
    } else
        return server1;
}

main.cpp

#include "ServerInterface.cpp"

int main() {

    ServerInterface * server = ServerInterface::getServer();

    return 0;
}

When I run this code, it goes inside getServer()'s if function and when it tries to get a pointer from the Server class I receive an error saying: cannot convert 'Server*' to 'ServerInterface*' in initialization. Previously, before I added the Server files and only HMDServer was included, it worked flawlessly. What did I do wrong?

IntiM
  • 11
  • 6
  • Isn't `ServerInterface * getServer()` supposed to be `ServerInterface * ServerInterface ::getServer()` in your ServerInterface.cpp file? you seem to have left out the class name in your implementation. Furthermore, `HMDServer * getHMDServerPtr()` is declared non-static, yet you invoke it as if it is static, and the implementation is completely missing from your post. **Post real code**. – WhozCraig Jul 14 '16 at 09:54
  • Please show **full** code. How does ServerInterface.cpp know about Server? – n. m. could be an AI Jul 14 '16 at 09:56
  • Thanks WhozCraig for your answer, that was already implemented in my actual code, I just forgot to include it here. I cannot post the entire code unless you want to look at 50 different functions that the server objects do that are irrelevant to the construction itself of the object such as getTime(), etc... The relevant functions are here and I've added #include files and main.cpp along with the corrections you pointed out. – IntiM Jul 14 '16 at 10:25
  • You forgot to provide code relevant to understanding your problem here? How, then, do you expect people to help you? Try creating an MCVE - a minimal compilable verifiable example - that actually exhibits the symptoms in a way that other people can reproduce. – Peter Jul 14 '16 at 11:25
  • You have memory leaks in your code, you should use smart pointers or be more accurate or both. – Slava Jul 14 '16 at 14:11

1 Answers1

0

Not sure if this is a typo or actually in your code, but in your ServerInterface.cpp, you state the following:

#include "HMDServer.cpp"
#include "Server.cpp"

If you truly include .cpp files instead of the actual header, strange behavior is to be expected. The same holds for

#include "ServerInterface.cpp"

in your main.cpp.

Felix Lauer
  • 201
  • 1
  • 10