How to compile .cpp codes that have separate interfaces and implementations in g ++?

2

I am studying C ++ for the book of Deitel , and I am trying to compile a program where we have a file gradebook.h which is the interface, gradebook.cpp which is the implementation and test_gradebook_header_file.cpp which is the code that contains the main function and the instance of the GradeBook class.

You're giving an error:

  

undefined reference to GradeBook :: GradeBook

And this is the same as the rest of the member functions, I did everything as it teaches, I've already checked for typos, but that's all right. I assume the problem is related to linking. I want to use g++ to do this but I do not know the correct steps to follow.

Here is the interface:

#include <string>

using namespace std;

// interface da classe GradeBook
class GradeBook
{
public:
    GradeBook(string name_course_param);
    void setCourseName(string name_course_param);
    string getCourseName();
    void displayMessage();
private:
    string courseName;
}; 

Here's the implementation:

#include <iostream>
#include "gradebook.h"

using namespace std;

GradeBook::GradeBook(string name_course_param)
{
    setCourseName(name_course_param);
}

void GradeBook::setCourseName(string name_course_param)
{
    courseName = name_course_param;
}

string GradeBook::getCourseName()
{
    return courseName;
}

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

Here is the program for testing:

// Utilizando a classe GradeBook incluindo o header file gradebook.h
#include <iostream>
#include "gradebook.h" // incluindo interface da classe

using namespace std;

int main(void)
{
    // criando duas instâncias da classe GradeBook
    GradeBook gradebook1("CS101 Introduction to C++ Programming");
    GradeBook gradebook2("CS102 Data Structures in C++");

    cout << "gradebook1 created for course: " << gradebook1.getCourseName()
      << "\ngradebook2 created for course: " << gradebook2.getCourseName()
      << endl;
}
    
asked by anonymous 13.09.2016 / 05:55

1 answer

2

You are not compiling the implementation of the class ...

g++ -o gradebook gradebook.cpp main.cpp
    
14.09.2016 / 01:23