Linux で g++ を使用してこれをコンパイルすると、エラー In function main':
main.cpp:(.text+0x42): undefined reference to
GradeBook::GradeBook(std::string)' collect2: error: ld returned 1 exit status が発生します
このコマンドを使用してコンパイルしています:
g++ -o gradebook.cpp gradebook.h main.cpp
// gradebook.cpp
#include <iostream>
using namespace std;
#include "gradebook.h"
GradeBook::GradeBook( string name )
{
setCourseName( name );
maximumGrade = 0;
}
void GradeBook::setCourseName( string name )
{
if ( name.length() <= 25 )
courseName = name;
else
{
courseName = name.substr( 0, 25 );
cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
<< "Limiting courseName to first 25 characters.\n" << endl;
}
}
string GradeBook::getCourseName()`
{
return courseName;`
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl;
}
int maximum( int, int, int );
int maximumGrade;`
void GradeBook::inputGrades()
{
int grade1;
int grade2;
int grade3;
cout << "Enter three integer grades: ";
cin >> grade1 >> grade2 >> grade3;
}
int GradeBook::maximum( int x, int y, int z )`
{
int maximumValue = x;
if ( y > maximumValue )
maximumValue = y;
if ( z > maximumValue )
maximumValue = z;
return maximumValue;
}
void GradeBook::displayGradeReport()
{
cout << "Maximum of grades entered: " << endl;
}
// gradebook.h
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook (string);
void setCourseName (string);
string getCourseName ();
void displayMessage();
void inputGrades ();
void displayGradeReport ();
int maximum (int, int, int);
private:
string courseName;
int maximumGrade;
};
// main.cpp
#include "gradebook.h"
int main (int argc, char*argv[])
{
GradeBook myGradeBook ("Introduction to C++");
return 0;
}