-1

My code blocks complier give 'undefined reference error to vtable Plant' to my code I have already read oteher articles in this page, but that did not resolved my issue. I tried to add both virtual and normal destructor. Please review my code and try to run it in Code::Blocks with cpp11 and this topic is not a duplicate! Here my code :

#include <iostream>    
#include <vector>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <time.h>

#include "Plant.h"



using namespace std;

int main(){



            vector<Plant*> novenyek;
            int novenyszam = 5;
            f >> novenyszam;
            novenyek.resize(novenyszam);
          //  novenyek[i].push_back(new Deltafa("a",0,true)); //thhis does not work 
            for (int i=0; i<novenyszam; i++){
               /* string tempname;
                string tempfajta;
                string temptapanyag;
            */
              //  string a = "Falank";
     // novenyek[i] =  new Puffancs(a,7,true);

      novenyek[i] = new Deltafa("a",0,true);

              //new Puffancs("Falank",7,true);

        //novenyek.push_back(new Puffancs("a",7,true));
//      novenyek[i] = new Plant(Puffancs("nev",7,true));

           //  novenyek.push_back();

            }
    return 0;
}

Plant.h

#pragma once
#include <string>
#include <ostream>
#include <stdlib.h>


enum Sugarzas
{
    ALFA,
    DELTA,
    NONE,
};

struct SugarzasIgeny
{
    Sugarzas _sugarzas;
    int _mennyiseg;
    SugarzasIgeny(Sugarzas sugarzas, int mennyiseg = 0): _mennyiseg(mennyiseg), _sugarzas(sugarzas) {}

};

class Plant
{
public:
    ///Plant(const std::string &nev, int tapanyag, bool El_e) : _nev(nev), _tapanyag(tapanyag), _El_e(El_e) {}


        std::string nev() const { return _nev; }
        int tapanyag() const { return _tapanyag; }
        bool El_e() const { return _El_e; }

        // Sablonfuggveny
        virtual SugarzasIgeny nap(Sugarzas sugarzas);
      ~Plant () {}
protected:


    std::string _nev; //noveny neve
    int _tapanyag;    //tapanyaga
    bool _El_e;       //ele

    Plant(const std::string &nev, int tapanyag, bool El_e) : _nev(nev), _tapanyag(tapanyag), _El_e(El_e) {}
};


class Puffancs : public Plant
{
public:
    Puffancs(const std::string &nev, int tapanyag, bool El_e) : Plant(nev, tapanyag, El_e) {}
    virtual  SugarzasIgeny nap(Sugarzas sugarzas) ;
};

class Deltafa : public Plant
{
public:
    Deltafa(const std::string &nev, int tapanyag, bool El_e) : Plant(nev, tapanyag, El_e) {}
    virtual  SugarzasIgeny nap(Sugarzas sugarzas) ;
};

class Parabokor : public Plant
{
public:
    Parabokor(const std::string &nev, int tapanyag, bool El_e) : Plant(nev, tapanyag, El_e) {}
    virtual SugarzasIgeny nap(Sugarzas sugarzas) ;
};

Plant.cpp

#include "Plant.h"

SugarzasIgeny Puffancs::nap(Sugarzas sugarzas)
{

    switch (sugarzas)
    {
    case Sugarzas::ALFA :
        _tapanyag += 2;
        break;
    case Sugarzas::DELTA :
        _tapanyag -= 2;
        break;
    case Sugarzas::NONE :
        _tapanyag -= 1;
        break;
    default:
        break;
    }

    if ( _tapanyag > 0 && _tapanyag <=10){
         _El_e = false;
    }else {
        _El_e = true;
    }


    return SugarzasIgeny(Sugarzas::ALFA, 10);
}

SugarzasIgeny Deltafa::nap(Sugarzas sugarzas)
{
    switch (sugarzas)
    {
    case Sugarzas::ALFA :
        _tapanyag -= 3;
        break;
    case Sugarzas::DELTA :
        _tapanyag += 4;
        break;
    case Sugarzas::NONE :
        _tapanyag -= 1;
        break;
    default:
        break;
    }
    if (_tapanyag > 0)
        {_El_e = true;}
    else {_El_e = false; }

    if (_tapanyag <5 ){
        return SugarzasIgeny(Sugarzas::DELTA, 4);
    }
    else if (_tapanyag >= 5 && _tapanyag <= 10 ){
        return SugarzasIgeny(Sugarzas::DELTA, 1);
    }else {
         return SugarzasIgeny(Sugarzas::NONE, 10);
    }

}

SugarzasIgeny Parabokor::nap(Sugarzas sugarzas)
{
    switch (sugarzas)
    {
    case Sugarzas::ALFA :
        _tapanyag++;
        break;
    case Sugarzas::DELTA :
        _tapanyag++;
        break;
    case Sugarzas::NONE :
        _tapanyag--;
        break;
    default:
        break;
    }
   // _El_e = tapanyag > 0;

    if (_tapanyag > 0){_El_e = true;}
    else {_El_e = false;}

    return SugarzasIgeny(Sugarzas::NONE, 10);
}
  • 2
    You never defined the "SugarzasIgeny" in the Plant class. Either do that or make it pure virtual! – Bayleef May 24 '20 at 15:29
  • Now I defined it but I still get the same error. – Vendor5599 May 24 '20 at 15:46
  • 1
    Post your error please, or at least give some hints on the line where exactly you get an error. – Hack06 May 24 '20 at 15:52
  • @Vendor5599 how did you defined it? – Jean-Baptiste Yunès May 24 '20 at 15:53
  • Code Blocks error: undefined reference to vtable for Plant when i try to use vector novenyek; novenyek.push_back(new Puffancs("a",7,true)); I have already read Stack owerflow links in this topic but none of them help me. – Vendor5599 May 24 '20 at 15:55
  • vector novenyek; Sugarzas napiSugarzas = Sugarzas::NONE; Sugarzas elozoNap = Sugarzas::ALFA; SugarzasIgeny alfaIgeny(Sugarzas::ALFA,1); SugarzasIgeny deltaIgeny(Sugarzas::DELTA,1); novenyek.push_back(new Puffancs("a",7,true)); for (int i=0; i<1; i++){ novenyek[i] = new Puffancs ("a",7,true); } – Vendor5599 May 24 '20 at 15:55
  • The plant destructor should be virtual in this instance. You will run into problems when deleting the pointers. By problems I mean the abyss of undefined behavior. – Bayleef May 24 '20 at 16:20

1 Answers1

0

Just define Plant::nap:

class Plant {
public:
    ...
    // Probably stupid code, but just for example
    virtual SugarzasIgeny nap(Sugarzas sugarzas) { return SugarzasIgeny(Sugarzas::ALFA); }
...
};

or make it pure virtual:

class Plant {
public:
    ...
    // Pure virtual, no need for code, but class is then non instanciable
    virtual SugarzasIgeny nap(Sugarzas sugarzas)=0;
...
};
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69