Jump to content
  • Advertisement
Sign in to follow this  
KieranW

C++: Unresolved external symbol on Class Constructor

This topic is 3557 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

Ok I have a class named Interface and here is the code for it. Interface.h
#ifndef MYSTICS_INTERFACE_H
#define MYSTICS_INTERFACE_H

#ifdef _MSC_VER
#define INLINE __forceinline
#else
#define INLINE inline
#endif

typedef int bool_t;
#define B_TRUE 1
#define B_FALSE 0

class Interface
{
public:

	INLINE Interface(void);
	INLINE virtual ~Interface(void);
	INLINE void capture(void);
	INLINE bool_t release(void);

private:

	unsigned int m_refCount;

};

#endif
Interface.cpp
#include "Interface.h"

Interface::Interface(void)
{
	m_refCount = 1;
}

Interface::~Interface(void)
{
}

void Interface::capture(void)
{
	m_refCount++;
}

bool_t Interface::release(void)
{
	m_refCount--;
	if(m_refCount == 0)
	{
		delete this;
		return B_TRUE;
	}
	return B_FALSE;
}
When compiled I get this error: Main.obj : error LNK2001: unresolved external symbol "public: __thiscall Interface::Interface(void)" (??0Interface@@QAE@XZ) Debug/__Mystics.exe : fatal error LNK1120: 1 unresolved externals Now, I have searched google and allsorts for answers but most just say that this is caused by a missing library file or a difference in parameters between the constructors definition and the actual code in the .cpp file. However I do not have any libraries that go with this class, and I have checked the parameters on the constructors.

Share this post


Link to post
Share on other sites
Advertisement
Inline functions must be implemented in the same file they are declared. Either drop the inline or put the function's body in the header file.

Share this post


Link to post
Share on other sites
If you want to declare the functions as inline you should implement them in the header. In fact if you include the implementation directly inside the class it will be implicitly inline. C++ has a bool type.

Be extremely careful with options like __forceinline unless you are 1000% sure of what you are doing. The compiler knows a lot more about optimisation than you, don't try to do its job for it.

Example:

#ifndef MYSTICS_INTERFACE_H
#define MYSTICS_INTERFACE_H

class Interface
{
public:
Interface()
:
refCount(1)
{
}

virtual ~Interface() {}

void capture();

bool release();

private:
unsigned int refCount;
};

inline void Interface::capture()
{
++refCount;
}

#endif

// Interface.cpp

#include "Interface.h"

bool Interface::release()
{
refCount--;

if(refCount == 0)
{
delete this;
return true;
}

return false;
}

Share this post


Link to post
Share on other sites

If you declare a function or member method to be inline, any files that use that function must have access to its definition.

A quick fix would be to move all inline function definitions to Interface.h instead of the .cpp file.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!