0

im trying to build a simple element character type game in c++. Having an abstract class Personaje and a child class called Agua. Im having an error for undefined reference on `vtable for Agua` , im not understading the issue here. In advance thanks for your time. Im new in c++ and im trying to learn POO.

Personaje.h

#ifndef PERSONAJE_H_
#define PERSONAJE_H_

#include "../header.h"


class Personaje
{
public:
    string name;
    int vida;
    int energia;
    int escudo;
    Personaje(string n, int v, int en, int es)
    {
        name = n;
        vida = v;
        energia = en;
        escudo = es;
    }

    virtual int alimentar() = 0;
};

#endif

Agua.h

#ifndef AGUA_H_
#define AGUA_H_

#include "../../header.h"
#include "../../Personaje/Personaje.h"

class Agua : public Personaje
{

private:
    int vecesAlimentado = 0;

public:
    Agua(string n, int v, int en, int es)
        : Personaje(n, v, en, es){};
    int alimentar();
};

#endif

main.cpp

#include "header.h"

int main() {    
    Agua agua("kure", 80, 0, 1);
    
    return 0;
}

Agua.cpp

#include "agua.h"

int Agua::alimentar()
{
    if (energia + 10 <= 20)
    {
        energia += 10;
        cout << name << "fue alimentado con plancton.";
        return energia;
    }
    cout << this->name << "no puede ser alimentado.";
    return energia;
}

header.h

#ifndef HEADER_H_
#define HEADER_H_

using namespace std;

#include <iostream>
#include <time.h>
#include <limits>
#include <string>
#include "./Personaje/Personaje.h"
#include "./personajes/agua/agua.h"

#endif

error

/usr/bin/ld: /tmp/ccqv1IKK.o: in function Agua::Agua(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, int)': /home/kurepa/programming/AYP2/TP2/./personajes/agua/agua.h:15: undefined reference to vtable for Agua' /usr/bin/ld: /tmp/ccqv1IKK.o: in function Agua::~Agua()': /home/kurepa/programming/AYP2/TP2/./personajes/agua/agua.h:7: undefined reference to vtable for Agua' collect2: error: ld returned 1 exit status

  • You did not define the destructors explicitly? – pptaszni Nov 17 '20 at 15:32
  • I did not. Should I do it? The destructor of Personaje or Agua? – Tomas Centeno Nov 17 '20 at 15:33
  • Are you using Creator? https://stackoverflow.com/questions/1552069/undefined-reference-to-vtable-trying-to-compile-a-qt-project – mzimmers Nov 17 '20 at 15:35
  • Both. They should be virtual. Anyway, looks like your Agoa.cpp is not compiled/linked. How do you build your project? Also correct your code snippets, they are missing necessary includes. – pptaszni Nov 17 '20 at 15:35
  • There i edited the snippets. Im not using creator. Im compiling with ` g++ -g -fsanitize=address -o main main.cpp` on ubuntu. – Tomas Centeno Nov 17 '20 at 15:39
  • You need to compile (and link) all your .cpp files, not just main.cpp – UnholySheep Nov 17 '20 at 15:41
  • How i do that when i a have a folder for each class containing .cpp and .h. And on the root directory i have the main.cpp? – Tomas Centeno Nov 17 '20 at 15:43
  • 1
    In a pinch you can do `g++ -o main main.cpp Agua/Agua.cpp Personaje/Personaje.cpp`, but you should probably invest in a simple build system like Makefiles or CMake. – Botje Nov 17 '20 at 15:47
  • I manage to fix it with your answers. Like UnholySheep said i wasnt compiling all files. Thanks to all for your time. – Tomas Centeno Nov 17 '20 at 15:52
  • 3
    Typically, the vtable is emitted in the translation unit that compiles the definition of the first non-inline virtual function. In this case, that would be **Agua.cpp**. The error message is indicating that the the vtable is missing, because **Agua.o** is not being linked into the executable. – Eljay Nov 17 '20 at 15:53
  • Thanks Eljay for the aclaration. Now i understand the error. – Tomas Centeno Nov 17 '20 at 16:03

1 Answers1

0

try replacing

int alimentar();

with int alimentar(){}