Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to vtable when using interface

I've looked around, and I can't quite figure out where I'm going wrong, as I seem to be following the correct convention when using interfaces, but perhaps I'm overlooking something. The exact error I'm getting is:

undefined reference to `vtable for Icommand'

I've only just begun to seperate my classes and class declarations into separate header files, so perhaps I'm missing a preprocessor directive somewhere.

main.cpp:

#include <iostream>
#include <string>
#include <cstdlib>
#include "Icommand.h"

#include "Command.h"

using namespace std;

void pause();

int main(){


    Icommand *run = new Command("TEST");
    cout << run->getCommand() << endl;
    delete run;

    pause();
}

void pause(){
    cin.clear();
    cin.ignore(cin.rdbuf()->in_avail());
    cin.get();
}

Icommand.h:

#ifndef ICOMMAND_H
#define ICOMMAND_H

#include <string>
#include <vector>


class Icommand
{
    private:

    public:
        Icommand(){}
        virtual ~Icommand(){}
        virtual bool run(std::string object1) = 0;
        virtual bool run(std::string object1, std::string object2) = 0;
        virtual std::string getCommand() const;
};



#endif // ICOMMAND_H

Command.h:

#ifndef COMMAND_H
#define COMMAND_H

#include <string>
#include <vector>

#include "Icommand.h"

class Command : public Icommand {

    private:
        std::string command;
        std::vector<std::string> synonymns;
        Command(); // private so class much be instantiated with a command

    public:
        Command(std::string command) : command(command){}
        ~Command(){}
        bool run(std::string object1);
        bool run(std::string object1, std::string object2);
        std::string getCommand() const;


};
#endif // COMMAND_H

Command.cpp:

#include <string>
#include <vector>

#include "Command.h"

bool Command::run(std::string object1){
    return false;
}
bool Command::run(std::string object1, std::string object2){
    return false;
}
std::string Command::getCommand() const {return command;}
like image 442
Martyn Shutt Avatar asked Mar 17 '23 08:03

Martyn Shutt


1 Answers

In Icommand.h, replace

virtual std::string getCommand() const;

with

virtual std::string getCommand() const = 0;

to make it pure virtual. Then the compiler can generate a vtable for Icommand. Alternatively, implement Icommand::getCommand.

like image 179
Wintermute Avatar answered Mar 23 '23 06:03

Wintermute