J
Joao Serrachinha
I'm learning c++ and i can't build this example from a book:
//file GradeBook.h
#include <string>
using std::string;
class GradeBook
{
public:
GradeBook( string );
void setCourseName( string );
string getCourseName();
void displayMessage();
private:
string courseName;
};
//file 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;
}
//file fig003_13.cpp
#include "GradeBook.h"
#include <iostream>
using std::cout;
using std::endl;
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;
}
I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:
g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
or
g++ -Wall fig03_13.cpp -o fig03_13
/tmp/ccGH8FTv.o: In function `main':
fig03_13.cpp.text+0xb7): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
fig03_13.cpp.text+0x13d): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
fig03_13.cpp.text+0x17d): undefined reference to
`GradeBook::getCourseName()'
fig03_13.cpp.text+0x1ad): undefined reference to
`GradeBook::getCourseName()'
collect2: ld returned 1 exit status
Even if i include the preprocessor wrapper in the GradeBook.h file the
result is the same!
Why this happen, what's wrong!? i have a lot of samples that i can't
compile!
thanks in advance.
//file GradeBook.h
#include <string>
using std::string;
class GradeBook
{
public:
GradeBook( string );
void setCourseName( string );
string getCourseName();
void displayMessage();
private:
string courseName;
};
//file 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;
}
//file fig003_13.cpp
#include "GradeBook.h"
#include <iostream>
using std::cout;
using std::endl;
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;
}
I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:
g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
or
g++ -Wall fig03_13.cpp -o fig03_13
/tmp/ccGH8FTv.o: In function `main':
fig03_13.cpp.text+0xb7): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
fig03_13.cpp.text+0x13d): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
fig03_13.cpp.text+0x17d): undefined reference to
`GradeBook::getCourseName()'
fig03_13.cpp.text+0x1ad): undefined reference to
`GradeBook::getCourseName()'
collect2: ld returned 1 exit status
Even if i include the preprocessor wrapper in the GradeBook.h file the
result is the same!
Why this happen, what's wrong!? i have a lot of samples that i can't
compile!
thanks in advance.