(Edited Title to Reflect the Cause of the Problem)
OLD TITLE: Undefined symbols for architecture x86_64: (C++) - What is it if my constructor & member functions are correct?
Working on a project for class and wanted to first verify the code from the text book but it doesn't working so I can't proceed onto the assignment.
The other posts I've read on this topic either point to a problem with defining the content in the constructor body or a problem where the class was not included in the definition of the member function (i.e. GradeBook:: GradeBook() , in my case).
The files I have are:
- GradeBook.h
- GradeBook.cpp
- main.cpp
When I run a g++ main.cpp I get the following:
Undefined symbols for architecture x86_64:
"GradeBook::GradeBook(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main-3ed012.o
"GradeBook::getCourseName() const", referenced from:
_main in main-3ed012.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My files:
GradeBook.h
// GradeBook.h
#include <string>
class GradeBook // GradeBook class definition
{
public:
explicit GradeBook( std::string ); // constructor initialize courseName
void setCourseName( std::string ); // sets the course name
std::string getCourseName() const; // gets the course name
void displayMessage() const; // displays a welcome message
private:
std::string courseName; // course name for this GradeBook
}; // end class GradeBook
GradeBook.cpp
// GradeBook.cpp
#include <iostream>
// include definition of class GradeBook
#include "GradeBook.h"
using namespace std;
// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
: courseName( name ) // member initializer to initialize courseName
{
// empty body
} // 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() const
{
return courseName; // return object's courseName
} // end function getCourseName
// display a welcome message to the GradeBook user
void GradeBook::displayMessage() const
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
} // end function displayMessage
main.cpp
// main.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
// function main begins program execution
int main()
{
// create two GradeBook objects
GradeBook gradeBook1( "CS101 Instruction 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;
} // end main
Thanks ahead of time for any help!