c++ - 错误 : undefined reference to typeinfo while implementing interface pattern in C++

标签 c++ qt design-patterns namespaces

我正在尝试编写一个基于接口(interface)的文件加载器,它将以多种方式针对不同的文件类型实现。我尝试了我想到的或在堆栈/互联网上找到的每一种组合,但我不断收到错误。我做错了什么?

核心/loader.h:

#ifndef CORE_LOADER_H
#define CORE_LOADER_H

class LoaderInterface
{
    public:
        virtual bool reloadFile();
};

#endif // CORE_LOADER_H

核心/lodaer/own.h:

#ifndef CORE_LOADER_OWN_H
#define CORE_LOADER_OWN_H

#include "src/core/loader.h"

class Own : public LoaderInterface
{
    public:
        bool reloadFile();
};

#endif // CORE_LOADER_OWN_H

核心/加载器/own.cpp:

#include "src/core/loader/own.h"

bool Own::reloadFile(){
    return true;
}

链接器说:

(..)/qt/build-Foo-Desktop_Qt_5_7_0_GCC_64bit-Debug/own.o:-1: error: undefined reference to `typeinfo for LoaderInterface'

最佳答案

你应该在接口(interface)类中使用纯虚函数:

class LoaderInterface
{
    public:
        virtual bool reloadFile() = 0;
};

关于c++ - 错误 : undefined reference to typeinfo while implementing interface pattern in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41555775/

相关文章:

c++ - 使用Bazel编译QT应用程序时访问资源

java - "Factory Method"和 "using New to create an instance"之间有什么区别?

design-patterns - 将 SQL View 映射到 JPA 实体

design-patterns - 在 Stylus/Sass 中编写多个主题的设计模式?

c++ - 队列会发生什么? (c++)

c++ - 更改静态库的包含路径

c++ - 在系统函数中输入一个字符串

c++ - 声明数组导致错误

c++ - 在 QGraphicsView 中获取 QGraphicsScene 的可见区域

c++ - 如何将 txt 文件中的值插入 Qlist 并使用 QTablewideget