Linker error, but you're going to solve it with one shot

This topic is 2677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, guys.

I'm trying to create a header file holding constants and helper functions. These functions will get the range of an ID and other very simple calculation.

Well, so I wrote it as:

constants.h
...int checkIDrange(int ID) {	int ID_range;	if(ID >= PLAYER || ID < ENEMY)			ID_range = PLAYER;				return ID_range;}...

and then I tried to use it in a class implementation

PhysicsManager.cpp
void PhysicsManager::updateCollision() {...int entityA_ID_range = checkIDrange(entityA_ID);...}

and then VC++ 2008 gave me the following linker error:

1>PhysicsManager.obj : error LNK2005: "int __cdecl checkIDrange(int)" (?checkIDrange@@YAHH@Z) already defined in navinha003.obj
1>C:\Users\draco\Documents\Visual Studio 2008\Projects\navinha\Debug\navinha003.exe : fatal error LNK1169: one or more multiply defined symbols found

In bold you will find the error numbers. Obviously I'm not inserting the checkIDrange function correctly, but I can't figure what it is the correct way either..

Share on other sites
Try
#pragma once//h file code

or
#ifndef MYH_FILE_H_#define MYH_FILE_H_//h file code#endif

Share on other sites
Function definitions in a header need to be marked inline.

Share on other sites
Quote:
 Original post by PromitFunction definitions in a header need to be marked inline.

I knew you guys would nail it with one shot! the question is: why the function need to be inlined? at every header file insertion is the function being defined again?

Skwee, I was using #pragma once already.

Share on other sites
Quote:
 Original post by draconarat every header file insertion is the function being defined again?
Bingo.