Tuesday, July 14, 2009

Visual C++ Express 08 error LNK2019?

Recently i took it upon myself to learn c++, and i have this great book called C++ How To Program 5th edition (publisher deitel)


My problem is that, when im doing one of the GradeBook projects I keep getting this error (For those of you with the book chap 3, pg. 100)


What im trying to learn is seperating Interface from Implementation.





I made my GradeBook.h, file this has the member function (and my singular data member) prototypes


I then made my GradeBook.cpp, this has my Definitions of the member functions


Lastly i have the drive file or the one that has 'main' in it.


I used the example of the disc, but every time i compile it i get an error





error LNK2019: unresolved external symbol "public: void __thiscall GradeBook::setCourseName(class std::basic_string%26lt;char,struct std::char_traits%26lt;char%26gt;,class std::allocator%26lt;char%26gt; %26gt;)" (?setCourseName@GradeBook@@QAEXV?$basic_... referenced in function _main Gradebook1.obj


Is this VC++?

Visual C++ Express 08 error LNK2019?
Microsoft describe this error here-


http://msdn2.microsoft.com/en-us/library...





Compiling a C/C++ program works in steps, first you compile the source code to object (obj) files made of symbols and code, then the linker stitches these obj files together to form your program. Some obj files use code from other obj files, they do this by using symbolic links to the code held in the other obj files.





Let's break the error down-





The GradeBook::setCourseName function is required by the main function in mainGradebook1.obj, but the symbol is not found within any other object files.





This is usually caused by 2 things-





1) GradeBook.cpp was not compiled into an object file, therefore the CPP code for GradeBook::setCourseName is not available.


Is GradeBook.cpp part of your project?


Was the obj file created? (search for it)





2) GradeBook.cpp was compiled, but the definition for the setCourseName function looks different to the declaration of the function within GradeBook.h. So when main.obj tries to use setCourseName it's looking for a different symbol (the name and arguments given by GradeBook.h) than the one provided by GradeBook.cpp, so it won't link.





The signatures of the function must match exactly between definition and declaration, even a const qualifier can mess it up. They should probably be like this-





// h, inside the GradeBook class body


virtual void setCourseName(std::string whatever);





// cpp


void GradeBook::setCourseName(std::string whatever) { }


No comments:

Post a Comment