Skip to main content
GameDev.net gamedev.net
🔒 Locked

Inline causing linker errors

Started by timmay314 Jan 10, 2004 at 1:03 PM 3 replies 250+ views
Original Post
timmay314
timmay314
I''m creating a particle system. I have a CParticle class and a CParticleSystem class, and to make things fast I want CParticle''s Update method to be inline. However, I get this linker error when I compile: ParticleSystem.obj : error LNK2001: unresolved external symbol "public: bool __thiscall CParticle::Update(void)" (?Update@CParticle@@QAE_NXZ) I had it working fine for a while, but I changed quite a bit of stuff, then I realized it was better before so I changed it back, and now I''m getting this error. Here is some code:
// This is CParticle

class CParticle  
{
public:
	CParticle();
	virtual ~CParticle();

	D3DXVECTOR3 m_vPos;
	D3DXVECTOR3 m_vVel;
	int m_iLife;
	bool m_bExists;

	void Create();
	inline void Update();
};

// This is how Update() is defined

inline void CParticle::Update()
{
	float fElapse = 1.0f / FRAMES;
	m_vVel.x += 0.01f * (rand() % 11) - 0.05f;
	m_vVel.y += 0.01f * (rand() % 11) - 0.05f;
	m_vVel.z += 0.01f * (rand() % 11) - 0.05f;
	m_vPos += fElapse * m_vVel;
	
	m_iLife -= 1000 / FRAMES;
	
	if(m_iLife <= 0)
		m_bExists = false;
}

// This is the only place that calls CParticle::Update()

void CParticleSystem::Update()
{
	CParticle* itParticle = m_aParticles;
	CParticle* end = m_aParticles + m_iMaxCount;
	for(; itParticle != end; itParticle++)
	{
		if(itParticle->m_bExists)
			itParticle->Update();
	}
}
I haven''t used inline functions very often so the problem could be something stupid, but I can''t think of anything I changed that would make it go from working to not working. Any ideas of what''s going on?
pjcast
pjcast
What would cause an unresolved external wile using inline would be something along these lines:

You declare a member function inline in the class header file...

You put the function body in a implementation cpp file.

You then procede to call that function from a cpp file other then the one you put the function body in.

Inline class functions must be defined within a header file because it is the compilers job to place inline function code into everyplace that calls it, not the linker. Any when it''s in an external cpp file, the compiler sees the defintion, but no code, so it waits for the linker to sort it out. But since the linker can not place the code into where it was called from, it sees an unresoved external.

Solution: Put the function into the header file and not the cpp file. Also note, that by default, all functions implemented in a class header file are usaully inlined by the compiler (even without inline keyword)

One note of caution: Inlining functions can result in speed/code bloat problems if abused...
weddo
weddo
While others have already explained what the problem is I only wanted to mention that using __forceinline instead of inline on MSVC compilers adds better inlining, but be carefull when inlining functions, only short often used ones or very short ones inside loops (where unavoidable to put the call inside the loop only) to keep the cache coherence and some few other things.
Miserable
Miserable
quote:
Original post by Anonymous Poster
If you want it inline, you have to put the implementation in the class definition.

No, you don''t.

That having been pointed out, previous posters have already supplied answers to the OP''s question.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.