Jul 9, 2011 at 10:15am UTC
hi.
i am new in c++ .
i m reading "How to c++ deitel & deitel".
in chapter 3 there is a proj;
there are 3 file in it:
fig03_13.cpp
--------
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
#include "GradeBook.h" // include definition of class GradeBook
int main()
{
GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
GradeBook gradeBook2( "CS102 Data Structures in C++" );
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;
return 0;
}
GradeBook.cpp
---------
#include <iostream>
using std::cout;
using std::endl;
#include "GradeBook.h"
GradeBook::GradeBook( string name )
{
setCourseName( name );
}
void GradeBook::setCourseName( string name )
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
}
GradeBook.h
--------------
#include <string>
using std::string;
class GradeBook
{
public:
GradeBook( string );
void setCourseName( string );
string getCourseName();
void displayMessage();
private:
string courseName;
};
**********************
when i am compiling ,VS says this error:
fig03_13.cpp.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall GradeBook::getCourseName(void)" (?getCourseName@GradeBook@@QAE?AV?$basic_string@DU ?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
1>B.obj : error LNK2019: unresolved external symbol "public: __thiscall GradeBook::GradeBook(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0GradeBook@@QAE@V?$basic_string@DU?$char_traits @D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
1>c:\documents and settings\hd\my documents\visual studio 2010\Projects\B\Debug\B.exe : fatal error LNK1120: 2 unresolved externals
1>
1>Build FAILED.
********
SO do you know what is the problem???
thank you for helping...
Jul 9, 2011 at 10:22am UTC
You probably didn't add gradebook.cpp to your project.
Jul 9, 2011 at 11:39am UTC
thank you for answers.
If you're using vs 2006 then...
VS version is 2010
i tested it in `code::blocks` too.
You probably didn't add gradebook.cpp to your project.
i copied "GradeBook.cpp" & "GradeBook.h" in same "fig03_13.cpp" folder.
by the way:
it works when i put all class in "GradeBook.h".
but when separate Interface from Implementation , it errors such as before.
maybe the wrong is with my project sort . i `ve chosen "console application" ?
...
Last edited on Jul 9, 2011 at 11:40am UTC
Jul 9, 2011 at 12:48pm UTC
It is not enough to put them in one folder (in fact, no one is forcing you to have everything in one folder). You have to tell VC++ what files to compile. I don't know if it's the same in VC++ 2010, but in 2008 one way to do that is to go to Project->Add Existing Item
Last edited on Jul 9, 2011 at 12:50pm UTC
Jul 9, 2011 at 2:10pm UTC
oh yeaaaaaaaaaaaaaaaaaaaaa!!!!
it solved .
dear "hamsterman" was right.
in fact i was 2 mistake.
1- i must use "Project->Add Existing Item " for my other file that added.
2- i must put #include "stdafx.h" to file: "GradeBook.cpp" too.( maybe VS always want this including in .cpp files.)
thank u.
gooooood luck