Working through Classes for beginners

I have the following code from C++ How to Program and it gives the following error when compiling.
I have copied the three files from the book and checked them over for spelling mistakes etc but can not find any, but that doesn't mean there aren't any!!
Can anyone help please...!

James--Sarahs-iMac:Chapt 3 james$ g++ fig3.13.cpp -o fig3.13
Undefined symbols for architecture x86_64:
"GradeBook::getCourseName()", referenced from:
_main in fig3-f348e7.o
"GradeBook::GradeBook(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in fig3-f348e7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
James--Sarahs-iMac:Chapt 3 james$



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  // Fig. 3.11: GradeBook.h
// GradeBook class definition. This file presents GradeBook's public
// interface without revealing the implementations of GradeBook's member
// functions, which are defined in GradeBook.cpp.
#include <string> // class GradeBook uses C++ standard string class
using namespace std;

// GradeBook class definition
class GradeBook
	{
	public:
		GradeBook( string ); // constructor that initializes courseName
		void setCourseName( string ); // function that sets the course name
		string getCourseName(); // function that gets the course name
		void displayMessage(); // function that displays a welcome message
	private:
		string courseName; // course name for this GradeBook
	}; // end class GradeBook



// Fig. 3.12: GradeBook.cpp
// GradeBook member-function definitions. This file contains
// implementations of the member functions prototyped in GradeBook.h.
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
		{
		setCourseName( name ); // call set function to initialize courseName
		} // end GradeBook constructor

		// function to set the course name
		void GradeBook::setCourseName( string name )
		{
		courseName = name; // store the course name in the object
		} // end function setCourseName

		// function to get the course name
		string GradeBook::getCourseName()
		{
		return courseName; // return object's courseName
		} // end function getCourseName

		// display a welcome message to the GradeBook user
		void GradeBook::displayMessage()
		{
		// call getCourseName to get the courseName
		cout << "Welcome to the grade book for\n" << getCourseName()
		<< "!" << endl;
		} // end function displayMessage


// Fig. 3.13: fig03_13.cpp
// GradeBook class demonstration after separating
// its interface from its implementation.
#include <iostream>
#include "GradeBook.h"  // include definition of class GradeBook
using namespace std;

// function main begins program execution
int main()
	{
		// create two GradeBook objects
		GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
		GradeBook gradeBook2( "CS102 Data Structures in C++" );

		// display initial value of courseName for each GradeBook
		cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
		<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
		<< endl;
		
		return 0;
	}//endmain
I have worked it out :) eventually... Well I am new to all this! It is a linker problem and I hadn't used the arguments correctly for g++ to compile.

Apologies for wasting anyone's time :(
Apologies for wasting anyone's time :(


Not at all, great to see you figured out something for yourself - which is a whole better than some others :+)

Some advice about compiling from the command line:

Set the warnings to a high level:

g++ -std=c++14 -Wall -Wextra -pedantic fig3.13.cpp -o fig3_13

This uses c++14 standard - your compiler might not have that capability. If not use -std=c++11 for the c++11 standard.

-Wall -Wextra -pedantic

These are some the warning options. You should always compile with these.


If you have lots of time, have a read of the compiler manual - there are a zillion options, but it is instructive nonetheless.

Good Luck !!

Hi - thanks for the reply. Not entirely sure which compiler it is but I am using xcode 7.1.1

I have created an alias in my bash profile
alias g++="g++ -std=c++14 -Wall -Wextra -pedantic"

should do it!!


thanks again
Not entirely sure which compiler it is but I am using xcode 7.1.1


Ok, there should be somewhere you can set those compile options in XCode.

I see from the wiki, that XCode supports clang, if you can, try to use that - it's awesome :+) Mainly because of it nice, reasonably non cryptic compiler messages. Clang is also the frontrunner in terms of providing support for new c++standards - c++17 is on the production line, they often provide new functionality before the new standard is released. clang++ relies on g++ so the options are identical.

Their are some compiler options that aren't on with those options, but are handy nonetheless. Narrowing of type is a good one double to int say. There are others, like if you use an enum in a switch, it will warn if you don't provide a case for each enum

Good Luck !!
Thanks again!

I will take a look at man clang and digest - cheers!
Topic archived. No new replies allowed.