How to compile C++ code with separate interface? (Arq.h , Arq.cpp and main.cpp)

Asked

Viewed 44 times

0

I’m studying C++ for the Deitel book and I’m trying to compile a program where we have a gradebook file. h which is the interface, gradebook.cpp which is the implementation and main.cpp. Whenever I run the program it returns me indefinite reference errors. I already executed a tip from a colleague here in another post (g++ -o gradebook gradebook.cpp main.cpp), in this case does not return me the errors, however, also does not return me anything in the terminal. simply compile and finish. Follow the code:

main.cpp

#include <iostream>
#include "gradebook.h" // definição de classe GradeBook
using namespace std;

int main()
{
    //cria dois objetos GradeBook
    GradeBook gradeBook1( "CS101 Introduction to Programming in C++" );
    GradeBook gradeBook2( "CS102 Data Structures in C++ ");

    //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 valido)
    gradeBook1.setCourseName( "CS101 C++ Programming");

    //Exibe valor inicial de courseName para cada GradeBook
    cout << "\ngradeBook1's course name is: " << gradeBook1.getCourseName() 
         << "\ngradeBook2's course name is: " << gradeBook2.getCourseName()
         << endl;
    return 0;
}

gradebook.cpp

#include <iostream>
#include "gradebook.h" //Definição de classe GradeBook
using namespace std;

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

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 lenght (25). \n"
             << "Limiting courseName to first 25 characters.\n" << endl;
    }
}

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

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

gradebook. h


#include <string> // a classe GradeBook utiliza a classe de string padrao C++
using namespace std;

class GradeBook
{
    public:
        GradeBook( string ); // Construtor que inicializa courseName
        void setCourseName( string ); //configura o nome do curso
        string getCourseName(); // obtem o nome do curso
        void displayMessage(); // exibe mensagem de boas vindas
    private:
        string courseName; //nome do curso para este GradeBook
};
  • You have already tried to run the file with ./gradebook in the terminal?

  • Just run it on the terminal like @Aviana said with ./gradebook on linux and mac or gradebook windows only. To complement you indicate the names of all cpps when compiling with g++ in the terminal and the option -o is to specify the name of the generated program.

  • Maybe you don’t understand the purpose. Compiling the program can only be generating object files, those with extension. OBJ, if you run gcc -c gradebook.cpp. If you run gcc -o blue gradebook.cpp main.cpp the result will be creating blue.exe. That’s it. It will not run the blue.exe program, which is supposed to be saved and run perhaps on other machines. This is the goal of a program. It is not expected to be compiled each time before it is executed. It is not expected to be executed every time it is compiled

  • Now I understood. I am very beginner and I had not found another explanation about it. He gave it right here!! Very grateful to all!

No answers

Browser other questions tagged

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