I'm failry unexperience in C++ and VS, and I can't figure out what I'm doing wrong, and more importantly how to fix it.
I am working on a solution with multiple projects, and am now starting to use classes from one project in another. However, I'm getting this error:
tlibdecoder.lib(TDecCu.obj) : error LNK2001: unresolved external symbol "public: void __thiscall CURegister::registerCU(int,int,int)" (?registerCU@CURegister@@QAEXHHH@Z)
the problem seems to be the the function I'm using in TDecCu.cpp, which comes from the files CURegister.h and CURegister.cpp.
Atop of CURegister.cpp I included CURegister.h, so the function is defined (I can use it normally within the same project by jsut importing CURegister.h), but when I include the header into TDecCu.cpp (by adding #include "CURegister.h" in the top) it seems there is a problem.
Can somebody tell me how to fix this? Keep in mind I'm inexperienced, so don't mind explaining it really simple ;)
Thanks in advance. B
An "unresolved symbol error" is a linker error, as opposed to a compiler error. Linking is the second stage of what appears to be a single "build" event in an IDE like Visual Studio.
The central problem here concerns the distinction between declarations and definitions in C++. In short, a declaration introduces a symbol (like a function) to the compiler, while a definition provides the actual instantiation/implementation of that symbol for the linker. When considering a function, the definition is what you've probably heard referred to as a prototype. Something like this:
void registerCu(int x, int y, int depth);
Notice that it does not provide the implementation for the function, so it is just a definition. That's different from a declaration, which provides the full implementation:
void registerCu(int x, int y, int depth)
{
// do magic stuff here
}
If this were a single, self-contained project, you could assume that the linker error was alerting you that you had failed to provide a definition of the function. That probably means you wrote a prototype in a header somewhere, but didn't actually write the contents of the function in a corresponding .cpp file.
But since your comments to infact's answer indicate that you have indeed provided the implementation in a .cpp file, and the question says that you're working with multiple projects, it's more likely that the second project is unable to find the function definition. You've included the header from the first project that provides the declaration to the second project, so the compiler is happy. But when the linker gets around to actually trying to link in the object code for that function call, it comes up disappointed because it can't find the definition.
Fix this by instructing the linker for the second project to look in the object code for the first project for any definitions that it can't find. You generally do this by adding the path to the folder that contains the build output for your first project to the linker settings for your second project.