1
I’m learning C++, and I have a question about creating classes in different files. I created the header with prototypes of functions, then as member functions in another cpp file.
When I go to test the files next to a main I have the following error:
undefined reference to GradeBook::GradeBook(std::string) linha 14
undefined reference to GradeBook::getNomeCurso() linha 16
This is the header with the gradebook class : gradebook. h
#include <iostream>
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook( string );
void setNomeCurso(string);
string getNomeCurso();
void mostrarMensagem();
private:
string nome_curso;
};
This is the file called gradebook.cpp with the member functions in it:
#include <iostream>
#include "gradebook.h"
using namespace std;
GradeBook::GradeBook( string z ){
setNomeCurso(z);
}
void GradeBook::setNomeCurso( string nome ){
nome_curso = nome;
}
string GradeBook::getNomeCurso(){
return nome_curso;
}
void GradeBook::mostrarMensagem(){
cout << "Bem vindo ao Livro de " << getNomeCurso() << "!" << endl;
}
And this is the test.cpp file with the Main function:
#include <iostream>
#include <string>
#include "gradebook.h"
using namespace std;
int main() {
string curso;
cout << "Entre com o nome do curso : ";
getline(cin, curso);
cout << endl;
GradeBook Livro( curso );
cout << "Bem vindo ao curso de " << Livro.getNomeCurso() << endl;
}
Which command was used to compile?
– Guilherme Bernal
Yeah, it looks like you didn’t compile/link the gradebook.cpp along with the.cpp test.
– C. E. Gesser
You need to generate the compiled object of the class before compiling the main program. Are you using Windows or Linux? Any IDE? Compiling at Hand?
– Henrique Barcelos
I am using codeblocs in windows, I will give a search on compiling the object, thank you
– Lucas