I recently started working with c++ after a few years in other languages and I am having trouble getting code to compile with constructors. I know the problem is the constructor because if I comment it out everything works fine. I have copied some code directly from a c++ book and I am still getting errors. I am compiling in the command line with g++.
The compiler error:
Undefined symbols for architecture x86_64:
"GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
_main in ccoPO1iA.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Any help would be greatly appreciated
gradeBookTest.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
GradeBook book1("newClass");
}
GradeBook.h
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook(string);
void setCourseName(string);
string getCourseName();
void displayMessage();
private:
string courseName;
};
GradeBook.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook(string name)
{
setCourseName(name);
}
void GradeBook::setCourseName( string name )
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::displayMessage()
{
cout << "this is the gradebook for\n" << getCourseName() << endl;
}
The indentation got messed up somehow when I copied the code into the browser…
You need to compile
GradeBook.cppas well along withgradeBookTest.cpp. This is happening because the compiler can see the constructor in the.hbut come linking time, the function definition of that constructor is nowhere to be seen.