Search code examples
c++vtable

undefined reference to 'vtable for'


So this is part of a bigger project. I have 3 different files. The first one is a class from a previous project that is just being used as a type (visitor.h) so I won't include it as I know it works. Then I have written an abstract class and another abstract class which inherits from the first one in space.h like so:

class space{

public:
    virtual int get_vis_count() const = 0;
    virtual int __enter(Visitor *v) = 0;
    virtual int search(Visitor *v) = 0;
    virtual Visitor* __exit(int index) = 0;
    virtual void print() const = 0;
    virtual ~space() { };

};

class capped_space: public space{

    public:
    virtual int get_capacity() const = 0;
    virtual int has_space() const = 0;
};

Now in my classes.h i have several classes and the error:

classes.o: In function `office::office(building*, int, int)':
classes.cpp:(.text+0x8fe): undefined reference to `vtable for office'
classes.o: In function `office::~office()':
 classes.cpp:(.text+0xe1a): undefined reference to `vtable for  office'

pops. classes.h:

class office:public capped_space{

private:
int office_number, No;
int  current_visitors;
int _f;
vector<Visitor*> visitors;

public:
office(building *b, int on, int m);
int get_capacity() const;
int has_space() const;
int get_vis_count() const;
int get_numb() const;
int __enter(Visitor * visitor);
int search(Visitor *v);
Visitor* __exit(int index);
Visitor* done();
void print() const;
~office();
};

classes.cpp:

office::office(building *b, int on, int m): office_number(on),  current_visitors(0), _f(m){
  No = b->get_space_size('o');
  cout << "Office number: " << office_number << "\thas been created\n";
}

int office::get_vis_count() const{
  return current_visitors;
}

int office::has_space() const{
  return (current_visitors < No);
}

int office::get_numb() const{
  return office_number;
}

int office::__enter(Visitor * visitor){
  if(visitor == NULL) return -1;
  if(has_space()){
    if(!visitor->__done()){
        if((visitor->get_office() == office_number)){
            visitors.push_back(visitor);
            cout << "Entering office " << _f << "-" << office_number << "\t: ";
            visitors[current_visitors]->print_v();
            current_visitors++;
            return 1;
        }else{
            return 0;
        }
    }else{
        return 1;
    }
}else{
    cout << "Please, wait outside for entrance in office number: " <<   office_number << endl;
    return 0;
  }
}

Visitor* office::__exit(int index){
  if (visitors[index]->__done()){
    Visitor *nav;
    nav = visitors[index];
    visitors.erase(visitors.begin() + index);
    current_visitors--;
    return nav;
  }else{
    return NULL;
  }
}

Visitor* office::done(){
  if(current_visitors){
    int random;
    random = rand() % current_visitors;
    visitors[random]->__set_done();
    cout << "Exiting office " << _f << "-" << office_number << "\t: ";
    visitors[random]->print_v();
    return __exit(random);
  }else{
    return NULL;
  }
}

void office::print() const{
    if(current_visitors != 0){
      cout << "Floor: " << _f << endl;
      cout << "Office: " << office_number << endl;
      for (int i = 0; i < No; i++)
      {
        visitors[i]->print_v();
      }
      cout << endl;
}
} 

office::~office(){
    cout << "End of the work!\n";
 }

The second error pops in class floor's functions but I hope that if I figure out the office error I will also figure out floor's.

If any more code is needed please comment and I will edit.

P.S. please ignore some ifs I may have left behind. The code was originally handling arrays of pointers and now I am trying to make it handle vectors of pointers so there may be some leftover checks


Solution

  • You are missing definitions for some of the virtual methods that you've declared in office, namely search and get_capacity.

    As a result, the compiler can't generate a virtual function table for office class because it doesn't have addresses for these virtual functions to put there. Hence a "very useful" error message, because, from compiler's perspective, the problem is that virtual table is missing and not why it's missing.