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

Asked

Viewed 191 times

0

Hello I would like a help with this exercise code. This is my first time trying to ask a question here.

When I try to compile the file with the member functions Gradebook.cpp 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 the post.

I cannot solve this problem nor understand why this error is happening.

Header file with the name of Gradebook. h (containing the prototypes of the member functions)

// 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;
};

Filing cabinet Gradebook.cpp with the class membership 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 Recebo este erro quando tento compilar o arquivo GradeBook.cpp

and this with the main fig03_17.cpp function file e este com o arquivo da função main fig03_17.cpp

1 answer

1


If there is more than one file cpp, all have to be compiled at the same time, usually starting with what has the main:

g++ fig03_17.cpp GradeBook.cpp

However if you have enough files it will be more appropriate to make a Makefile to facilitate and automate the creation of the executable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.