# LNK2005 Error

## Recommended Posts

WackyWormer    126
I'm getting a LNK2005 error in my program. This didn't happen until I moved a function from a class to a global scope, inside a different header file. This header file is included in 2 different places, and if i comment out one place, I don't get the link error. Here is the file i moved the function to, and thanks for the help
#ifndef COLORS_H
#define COLORS_H

const string CNRM = "\x1B[0m";
const string CRED = "\x1B[31m";
const string CGRN = "\x1B[32m";
const string CYEL = "\x1B[33m";
const string CBLU = "\x1B[34m";
const string CMAG = "\x1B[35m";
const string CCYN = "\x1B[36m";
const string CWHT = "\x1B[37m";
const string CNUL = "";

void writeColor(string Color, string& dest)
{

string temp;
temp = Color;
temp += dest;
temp += Color;
dest = temp;
}

#endif


##### Share on other sites
WackyWormer    126
ok I fixed it but i don't understand the difference. I created a .cpp for the header and move the function definition to it, and left the prototype in the .h file... And now no errors... Can anyone explain this?

##### Share on other sites
ConayR    122
Were you including this header in code files being part of more than one lib?

##### Share on other sites
WackyWormer    126
Well i'm somewhat of a beginner, so i'm not entirely sure what you're asking, but i created the file for one project, it is only being used in one project, and it is #included in 3 files in this project. if by lib files you mean .lib, i don't have a clue how to make those, so no

##### Share on other sites
scorpion007    118
Quite simply, you have a symbol that was multiply defined.
You cannot define a symbol twice.

The symbol, in your case, was the function writeColor(). You inadvertently defined it twice because you included the same header (containing this definition) in at least 2 files.
When both files were *compiled* (into .obj files) they both got a copy of the code of 'writeColor()' (or, the definition).
When the linker needed to form an executable from your .obj files (or, 'link your object modules together'), it found two definitions for the same symbol. It doesn't know which to use. Hence, the error. You have to tell it, by only defining any given symbol *once*.