0

I am having trouble writing a code in which a class that inherits from another class reuses a function of this class in one of its functions
file pessoa.h:

#ifndef PESSOA_H
#define PESSOA_H
#include "data.h"
#include <string>

class Pessoa
{
   string nome;
   Data aniversario;
public:
   Pessoa(string tnome, Data taniversario){nome = tnome; aniversario =       taniversario;}
Pessoa(){};
int Aniversario(Data data_atual); 
virtual void imprime();
~Pessoa();

};

#endif

file universitario.h:

#ifndef UNI_H
#define UNI_H
#include "pessoa.h"
#include "data.h"

class Universitario: public Pessoa
{
    int matricula;
    Data ingresso;

 public:
    Universitario(int tmatricula, Data tingresso, string nome, Data     aniversario): 
    Pessoa(nome, aniversario) {matricula = tmatricula; ingresso = tingresso;}
    Universitario(){};
    void imprime();
    ~Universitario();


};

#endif

function imprime() definition from pessoa.cpp:

void Pessoa::imprime()
{    
    cout << "Nome: " << this->nome << endl;

}

function imprime() definition from uni.cpp:

void Universitario::imprime()
{
    this->imprime();
    cout << "Matricula: " << this->matricula << endl;
    ingresso.imprimed();

}

The error I get is this:

Undefined symbols for architecture x86_64:
  "Pessoa::~Pessoa()", referenced from:
      Universitario::~Universitario() in main-0c1624.o
      Universitario::Universitario(int, Data,     std::__1::basic_string<char, std::__1::char_traits<char>,     std::__1::allocator<char> >, Data) in main-0c1624.o
  "typeinfo for Pessoa", referenced from:
      typeinfo for Universitario in main-0c1624.o
  "vtable for Pessoa", referenced from:
      Pessoa::Pessoa(std::__1::basic_string<char,    std::__1::char_traits<char>, std::__1::allocator<char> >, Data) in main-   0c1624.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am guessing the error is either on virtual or this.

1
  • void Universitario::imprime() { this->imprime(); is an infinite loop. Change this->imprime into Pessoa::imprime if all you want is first calling the base class method. Commented Apr 19, 2016 at 6:41

1 Answer 1

2
"Pessoa::~Pessoa()", referenced from:

You need to actually implement your destructor. You only declared it.

Instead of this:

~Pessoa();

Try this:

~Pessoa() {}

Same holds true for the Universitario destructor.

Sign up to request clarification or add additional context in comments.

4 Comments

I did that and got this: Undefined symbols for architecture x86_64: "typeinfo for Pessoa", referenced from: typeinfo for Universitario in main-0275b3.o "vtable for Pessoa", referenced from: Pessoa::~Pessoa() in main-0275b3.o Pessoa::Pessoa(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, Data) in main-0275b3.o NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocatio
@emic: are you sure you recompiled all your .cpp files, and specified the .os when linking? You should edit your compile steps in at the end of the question.
The destructor should probably be virtual and could be defaulted.
I forgot to include one of the files in the main file. Problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.