I am having problems with compiling some code in C++ which i am learing from Deitel and Deitels C++ how to program.
When trying to compile one of their examples i get the following errors

D:\Web Design\C++\C++ how to program\Fig03_15>g++ fig03_17.cpp
C:\DOCUME~1\SKILLI~1\LOCALS~1\Temp/ccUzb5iL.o:fig03_17.cpp:(.text+0x1aa): undefined reference to `GradeBook::GradeBook(std::string)'
C:\DOCUME~1\SKILLI~1\LOCALS~1\Temp/ccUzb5iL.o:fig03_17.cpp:(.text+0x283): undefined reference to `GradeBook::GradeBook(std::string)'
C:\DOCUME~1\SKILLI~1\LOCALS~1\Temp/ccUzb5iL.o:fig03_17.cpp:(.text+0x31a): undefined reference to `GradeBook::getCourseName()'
C:\DOCUME~1\SKILLI~1\LOCALS~1\Temp/ccUzb5iL.o:fig03_17.cpp:(.text+0x342): undefined reference to `GradeBook::getCourseName()'
C:\DOCUME~1\SKILLI~1\LOCALS~1\Temp/ccUzb5iL.o:fig03_17.cpp:(.text+0x48a): undefined reference to `GradeBook::setCourseName(std::string)'
C:\DOCUME~1\SKILLI~1\LOCALS~1\Temp/ccUzb5iL.o:fig03_17.cpp:(.text+0x521): undefined reference to `GradeBook::getCourseName()'
C:\DOCUME~1\SKILLI~1\LOCALS~1\Temp/ccUzb5iL.o:fig03_17.cpp:(.text+0x549): undefined reference to `GradeBook::getCourseName()'
collect2: ld returned 1 exit status

I am using 3 classes. They are as follows

//Fig 3.15: Gradebook.h
//GradeBook class definition presents the public interface
//Member functions appear in GradeBook.cpp

#include <string>
using std::string;

//GradeBook Class Definition
class GradeBook {
	public:
	GradeBook(string); //Constructor that initialises the GradeBook object
	void setCourseName( string ); //function that sets the coursename
	string getCourseName(); //function that gets the course name
	void displayMessage(); //function that displays a welcome message
	private:
	string courseName; //coursename for this GradeBook
}; //end class GradeBook.


//Fig 3.16 GradeBook.cpp
//This has functions which the Gradebook class uses for validating data.
#include <iostream>
using std::cout;
using std::endl;

#include "GradeBook.h" //include definition of class GradeBook.

//Function that sets the course name and makes sure that it is no more than 25 characters
void GradeBook::setCourseName( string name ){
//If length of string name is equal to or less than 25 characters set the course name
if (name.length() <= 25) 
	courseName = name;
	//If length of string name is greater than 25 characters return a notification
	if (name.length() >25 ){
		//Set the courseName to the first 25 characters of the string.
		courseName = name.substr( 0,25 ); //Start at zero position of string and end at position 25
		cout << "Name " << name << " exceeds maximum length (25) .\n"
		<< "Limiting courseName to first 25 characters .\n" <<endl;
	} //End if loop

} //End function setCourseName

//Function to get the course name
string GradeBook::getCourseName(){
    return courseName;
}

void GradeBook::displayMessage()
{
	cout <<"Welcome to the grade book for\n" <<getCourseName() 
	<< "!" << endl;
}

//Constructor initialises courseName with string supplied as an argument
GradeBook::GradeBook( string name ) {
	setCourseName (name ); //validate and store coursename
}


//Fig 3.17 fig03_17.cpp
//Create and manipulate a GradeBook object; illustrate validation

#include <iostream>
using std::cout;
using std::endl;

#include "GradeBook.h" //include the class GradeBook.h.

#undef main
//Main Function
int main(){
	//Create two gradebook objects
	//gradeBook1 object name is too long
	GradeBook gradeBook1("CS101 Introduction to programming in C++" );
	GradeBook gradeBook2("CS102 C++ Data Structures");
	
	//Display each GradeBooks courseName
	cout << "gradeBook1's initial course name is " <<
	gradeBook1.getCourseName() <<
	"\ngradeBook2's initial course name i: " <<
	gradeBook2.getCourseName() <<endl;
	
	//Modify myGradeBooks courseName witha valid length string
	gradeBook1.setCourseName("CS101 C++ Programming");
	
	//Display courseName string for both Gradebook objects
	cout << "\ngradeBook1's course name is " <<
	gradeBook1.getCourseName() <<
	"\ngradeBook2's course name is " <<
	gradeBook2.getCourseName() <<endl;
	return 0; //Successful termination of program
};

Anyone have any ideas, ive been trawling the net and cannot find any solutions.
I can only assume it is a parameter in my PATH as the text is pretty much a direct copy from the book

Recommended Answers

All 2 Replies

Try compiling with: g++ fig03_17.cpp Gradebook.cpp :)

commented: Good Catch +19
commented: I agree +17

Follow tux4life's instruction above.

But that is actually a linker error and what the error actually means is that you have compiled the file 'fig03_17.cpp' and made 'reference' to functions that exist in other files but have not been compiled themselves or have not been linked in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.