LNK2019 && LNK1120 errors when splitting my code in multiple files
c++, linker, visual-studio-2008
Solution
You declared two classes here. One of them is in myclass.h and the other is in myclass.cpp. Try the following instead:
myclass.h
#ifndef myclass_h_included
#define myclass_h_included
#include <tchar.h>
class MyClass {
private:
static bool someProperty;
static void doSomeOneTimeCode ();
public:
static bool MyFunction (TCHAR* someStringArgument);
};
#endif //!myclass_h_included
myclass.cpp
#include "myclass.h"
/*static*/ bool MyClass::someProperty = false;
void
MyClass::doSomeOneTimeCode() {
//...
}
bool
MyClass::MyFunction(TCHAR* someStringArgument) {
//...
}
Your main.cpp can stay the same. I would pay attention to UncleBens reply as well. One time initialization code should be hidden if at all possible.
Problem
My code is stored in a `main.cpp` file which contains the `void main()` function, and a class `MyClass` which I now want to split to another file. IDE is Microsoft Visual Studio 2008 Professional. `myclass.h` ``` #include <tchar.h> class MyClass { public: static bool MyFunction (TCHAR* someStringArgument); }; ``` `myclass.cpp` ``` #include <tchar.h> class MyClass { private: static bool someProperty; static void doSomeOneTimeCode () { if (!someProperty) { /* do something */ someProperty = true; } } public: static bool MyFunction (TCHAR* someStringArgument) { doSomeOneTimeCode(); /* do something */ return true; } }; bool MyClass::someProperty = false; ``` `main.cpp` ``` #include <windows.h> #include <stdio.h> #include <tchar.h> #include "myclass.h" void main () { if (MyClass::MyFunction(TEXT("myString"))) { _tprintf(TEXT("Yay\n")); } } ``` However, when I try to run it, I get two linker errors. - LNK2019: unresolved external symbol ... (mentions `MyClass::MyFunction`) - LNK1120: 1 unresolved externals What can I do to prevent these linker errors?