Error compiling C ++ source code together with header file and member function file

0

Hello, I would like some help with this exercise code. This is my first time trying to get a doubt here.

When I try to compile the file with the GridBook.cpp member functions or the file containing the main function (_fig03_17.cpp_) is returned with a compilation error as shown in the images at the end of post.

I can not solve this problem nor understand why this error is happening.

GradeBook.h header file (containing member function prototypes)

// Figura 3.15: GradeBook.h
// Definição de classe GradeBook apresenta a interface public da
// classe. Definições de função-membro aparecem em GradeBook.cpp.
#include <string>
using std::string;

// definição da classe GradeBook
class GradeBook
{
public:
    GradeBook( string );
    void setCourseName( string ); 
    string getCourseName(); 
    void displayMessage();
private:
    string courseName;
};

GradeBook.cpp file with class member functions

// Figura 3.16: GradeBook.cpp
// Implementações das definições de função-membro de GradeBook
// A função setCourseName realiza a validação.
#include <iostream>
using std::cout;
using std::endl;

#include "GradeBook.h" //inclui a definição de classe GradeBook

GradeBook::GradeBook( string name )
{
    setCourseName( name ); // valida e armazena courseName
} // fim do construtor GradeBook

void GradeBook::setCourseName( string name )
{
    if ( name.length() <= 25 )
        courseName = name; 

    if ( name.length() > 25 )
    {
        courseName = name.substr( 0, 25 );

        cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
            << "Limiting courseName to first 25 characters.\n" << endl;
    } // fim do if
} // fim da função setCourseName

string GradeBook::getCourseName()
{
    return courseName; // retorna o courseName do objeto
} // fim da função getCourseName

// exibe uma mensagem de boas-vindas ao usuário GradeBook
void GradeBook::displayMessage()
{
    // chama getCourseName para obter o courseName
    cout << "Welcome to the grade book for\n" << getCourseName()
        << "!" << endl;
} // fim da função displayMessage

File with the main function main ()

// Figura 3.17: fig03_17.cpp
// Cria e manipula um objeto GradeBook; ilustra a validação.
#include <iostream>
using std::cout;
using std::endl;

#include "GradeBook.h" // inclui a definição de classe GradeBook

// a função main inicia a execução do programa
int main()
{
    // cria dois objetos GradeBook;
    // nome inicial de curso de gradeBook1 é muito longo
    GradeBook gradeBook1( "CS101 Introduction to Programming in C++" );
    GradeBook gradeBook2( "CS102 C++ Data Structures" );

    // exibe courseName de cada GradeBook
    cout << "gradeBook1's initial course name is: "
        << gradeBook1.getCourseName()
        << "\ngradeBook2's initial course name is: "
        << gradeBook2.getCourseName() << endl;

    // modifica courseName do myGradeBook (com uma string de comprimento válido)
    gradeBook1.setCourseName( "CS101 C++ Programming" );

    // exibe courseName de cada GradeBook
    cout << "\ngradeBook1's course name is: "
        << gradeBook1.getCourseName()
        << "\ngradeBook2's course name is: "
        << gradeBook2.getCourseName() << endl;
    return 0; // indica terminação bem-sucedida
} // fim de main

I get this error when I try to compile the GradeBook.cpp file

andthiswiththemainfunctionfilefig03_17.cpp

    
asked by anonymous 07.08.2017 / 21:28

1 answer

1

If there is more than one cpp file, all must be compiled at the same time, usually beginning with the main :

g++ fig03_17.cpp GradeBook.cpp

However if you have enough files it will be more appropriate to do MakeFile to make it easier and automate the creation of the executable.

    
07.08.2017 / 21:51