Normally a person writes a separate header and cpp file. One exception to this I have seen (and done before) is when the class is a simple abstract class that is meant to be used as an interface.
I have done this a number of times without problems. Now I am getting an issue with the linker.
For example... here are two such abstract interfaces (and their non-abstract implementation) one of which compiles fine. In the other, the linker complains that the constructor and destructor are missing.
I give these two as an example of the problem because their implementation is almost identical and the client is the second file (out of 2 total) in which I have seen this problem occur. I fixed the first by creating a cpp file for the interface (because it really wasn't an interface). Not sure why that is necessary but whatever.
In the case with these two though, both the interface and their implementation have the same format. One works, the other doesn't. I will not include cpp files because the problem isn't with functions written in the cpp files. The problem is with the interface constructors/destructors (which are defined in the header in both cases). FYI, the client, server, and core are in separate dlls/libraries.
NOTE: This is a Qt project; hence all the Qt includes and keywords
The set that works fine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
// the interface...
#ifndef I_SERVERENGINE
#define I_SERVERENGINE
#include <qstring.h>
#include <qobject.h>
#include "appcore_global.h"
#include "enums.h"
namespace AppCore
{
class ILiveServerLogHost;
class NetEventInfo;
class APPCORE_EXPORT IServerEngine : public QObject
{
Q_OBJECT
protected:
IServerEngine() { };
~IServerEngine() { };
public slots:
virtual void loginRequest(NetEventInfo* requestEvent) = 0;
virtual void loginReceived(NetEventInfo* requestEvent) = 0;
virtual void loginSent(NetEventInfo* sendEvent) = 0;
public:
virtual void masterServerStart() = 0;
virtual void start(ServerRoles role) = 0;
virtual void setLiveServerHost(ILiveServerLogHost* host) = 0;
virtual ILiveServerLogHost* getLiveServerHost() = 0;
};
}
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
// the derived user of interface
#ifndef SERVERENGINE_H
#define SERVERENGINE_H
#include "../AppCore/enums.h"
#include "serverengine_global.h"
#include "../AppCore/IServerEngine.h"
#include "LoginServer.h"
namespace AppCore
{
class ILiveServerLogHost;
class SERVERENGINE_EXPORT ServerEngine : public IServerEngine
{
Q_OBJECT
public:
ServerEngine();
~ServerEngine();
public: // overrides
virtual void masterServerStart();
virtual void start(ServerRoles role);
virtual void setLiveServerHost(ILiveServerLogHost* host);
virtual ILiveServerLogHost* getLiveServerHost();
public slots:
virtual void loginRequest(NetEventInfo* requestEvent);
virtual void loginReceived(NetEventInfo* requestEvent);
virtual void loginSent(NetEventInfo* sendEvent);
protected:
bool flagIsSet(ServerRoles role);
private:
ILiveServerLogHost* mServerHost;
ServerRoles mServerRole;
LoginServer* mLoginServer;
};
}
#endif // SERVERENGINE_H
|
The set that DOES NOT work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
// the interface...
#ifndef I_CLIENTENGINE
#define I_CLIENTENGINE
#include <qstring.h>
#include <qobject.h>
#include "appcore_global.h"
namespace AppCore
{
class IContentHost;
class IBlockHost;
class IGeckoHost;
class IAssessmentHost;
class APPCORE_EXPORT IClientEngine : public QObject
{
Q_OBJECT
protected:
IClientEngine() { };
~IClientEngine() { };
public:
virtual void setContentHost(IContentHost* host) = 0;
virtual void setBlockHost(IBlockHost* host) = 0;
virtual void setGeckoHost(IGeckoHost* host) = 0;
virtual void setAssessmentHost(IAssessmentHost* host) = 0;
};
}
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
// the derived user of interface
#ifndef CLIENTENGINE_H
#define CLIENTENGINE_H
#include "clientengine_global.h"
#include "../AppCore/IClientEngine.h"
namespace AppCore
{
class IContentHost;
class IBlockHost;
class IGeckoHost;
class IAssessmentHost;
class CLIENTENGINE_EXPORT ClientEngine : public IClientEngine
{
Q_OBJECT
public:
ClientEngine();
~ClientEngine();
public: // overridden
virtual void setContentHost(IContentHost* host);
virtual void setBlockHost(IBlockHost* host);
virtual void setGeckoHost(IGeckoHost* host);
virtual void setAssessmentHost(IAssessmentHost* host);
private:
//static ClientEngine* singleton;
IContentHost* mContentHost;
IBlockHost* mBlockHost;
IGeckoHost* mWebBrowser;
IAssessmentHost* mAssessmentHost;
};
}
#endif // CLIENTENGINE_H
|