C++ 使用接口时对vtable的未定义引用

C++ 使用接口时对vtable的未定义引用,c++,inheritance,interface,polymorphism,C++,Inheritance,Interface,Polymorphism,我环顾四周,不知道哪里出了问题,因为我在使用接口时似乎遵循了正确的惯例,但也许我忽略了一些东西。我得到的确切错误是: 对“Icommand的vtable”的未定义引用 我刚刚开始将我的类和类声明分离到单独的头文件中,所以我可能在某个地方丢失了一个预处理器指令 main.cpp: #include <iostream> #include <string> #include <cstdlib> #include "Icommand.h" #include "Co

我环顾四周,不知道哪里出了问题,因为我在使用接口时似乎遵循了正确的惯例,但也许我忽略了一些东西。我得到的确切错误是:

对“Icommand的vtable”的未定义引用

我刚刚开始将我的类和类声明分离到单独的头文件中,所以我可能在某个地方丢失了一个预处理器指令

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();
}
#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
#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
#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;}
#包括
#包括
#包括
#包括“Icommand.h”
#包括“Command.h”
使用名称空间std;
无效暂停();
int main(){
Icommand*run=新命令(“测试”);
cout getCommand()在_avail()中;
cin.get();
}
Icommand.h:

#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();
}
#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
#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
#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;}
\ifndef ICOMMAND\u H
#定义ICOMMAND_H
#包括
#包括
类I命令
{
私人:
公众:
Icommand(){}
虚拟~Icommand(){}
虚拟bool运行(std::string object1)=0;
虚拟bool运行(std::string object1,std::string object2)=0;
虚拟std::string getCommand()常量;
};
#endif//ICOMMAND\u H
Command.h:

#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();
}
#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
#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
#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;}
#ifndef命令
#定义命令
#包括
#包括
#包括“Icommand.h”
类命令:公共Icommand{
私人:
std::string命令;
向量同义词;
Command();//私有类可以用命令实例化
公众:
命令(std::string命令):命令(Command){}
~Command(){}
bool运行(std::string object1);
bool运行(std::string object1,std::string object2);
std::string getCommand()常量;
};
#endif//COMMAND\u H
Command.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();
}
#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
#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
#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;}
#包括
#包括
#包括“Command.h”
bool命令::run(std::string object1){
返回false;
}
bool命令::run(std::string object1,std::string object2){
返回false;
}
std::string命令::getCommand()常量{return Command;}
在Icommand.h中,更换

virtual std::string getCommand() const;


使其纯粹虚拟化。然后编译器可以为
Icommand
生成vtable。或者,实现
Icommand::getCommand

您需要
virtual~Command(){}
是的,这已经解决了问题,尽管我仍然难以理解为什么它需要一个纯虚拟函数。我必须熟悉vtables。