继承问题

发布于 2024-10-11 05:27:43 字数 974 浏览 3 评论 0原文

class OGraph {
public:
    OGraph() { }
    virtual ~OGraph();

    virtual bool is_directed()=0;
};

class OUGraph : public OGraph {
public:
    OUGraph() {  }
    bool is_directed() { return false; }
    ~OUGraph() {}
};

但是当我这样做时,

OUGraph myGraph();
cout << myGraph.is_directed();

我收到以下错误:

未定义的符号:“typeinfo for OGraph”,引用自: OUGraphin main.o“OGraph 的 vtable”的 typeinfo,引用自: OGraph::OGraph()in main.o mkdir -p dist/Debug/GNU-MacOSX "OGraph::~OGraph()",引用自: g++-o 分布/调试/GNU-MacOSX/opengraph 构建/调试/GNU-MacOSX/OGraph.o 构建/调试/GNU-MacOSX/main.o
main.o 中的 OUGraph::~OUGraph() OUGraph::~OUGraph()in main.o ld: 未找到符号collect2: ld 返回 1 退出状态 make[2]: * [dist/调试/GNU-MacOSX/opengraph] 错误 1 ​​make[1]: [.build-conf] 错误 2 make: ** [.build-impl] 错误 2

构建失败(退出值 2,总计 时间:568ms)

class OGraph {
public:
    OGraph() { }
    virtual ~OGraph();

    virtual bool is_directed()=0;
};

class OUGraph : public OGraph {
public:
    OUGraph() {  }
    bool is_directed() { return false; }
    ~OUGraph() {}
};

but when I do this

OUGraph myGraph();
cout << myGraph.is_directed();

I get the following error:

Undefined symbols: "typeinfo for
OGraph", referenced from:
typeinfo for OUGraphin main.o "vtable for OGraph", referenced from:
OGraph::OGraph()in main.o mkdir -p dist/Debug/GNU-MacOSX "OGraph::~OGraph()", referenced from:
g++ -o
dist/Debug/GNU-MacOSX/opengraph
build/Debug/GNU-MacOSX/OGraph.o
build/Debug/GNU-MacOSX/main.o
OUGraph::~OUGraph()in main.o
OUGraph::~OUGraph()in main.o ld: symbol(s) not found collect2: ld
returned 1 exit status make[2]: *
[dist/Debug/GNU-MacOSX/opengraph]
Error 1 make[1]:
[.build-conf]
Error 2 make: ** [.build-impl] Error
2

BUILD FAILED (exit value 2, total
time: 568ms)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一笔一画续写前缘 2024-10-18 05:27:43

这是:

OUGraph myGraph();

声明一个名为 myGraph 的函数,该函数返回一个 OUGraph。您需要改为:

OUGraph myGraph;

这按照您的意图声明了一个变量。

This:

OUGraph myGraph();

is declaring a function called myGraph that returns a OUGraph. You need to instead have:

OUGraph myGraph;

This declares a variable, as you intend.

童话里做英雄 2024-10-18 05:27:43

这是所有 C++ 新人都会迷恋的老东西。

OUGraph myGraph();

您尚未创建名为 myGraph 的变量,而是声明了一个名为 myGraph 的函数,该函数返回 OUGraph。去掉“()”。

您显然也从未在任何地方定义过您的基地的析构函数。即使它是空的,您也需要定义它,因为您在类定义中声明了它。

It's the same old thing all new C++ people fall for.

OUGraph myGraph();

You've not created a variable called myGraph, you've declared a function called myGraph that returns an OUGraph. Remove the "()".

You also apparently never defined your base's destructor anywhere. Even if it's empty you'll need to define it since you declared it in your class definition.

雨后彩虹 2024-10-18 05:27:43

您没有为virtual ~OGraph();提供实现。要么提供一个实现,用 = 0; 将其指定为纯虚拟,或者干脆删除它。

编译器期望将类的类型信息附加到其第一个虚拟方法的实现中。您的第一个虚拟方法未定义,因此永远不会生成实现,并且您会收到相关的链接错误。

此外,正如大家所说,您应该从 myGraph 的定义中删除括号。

You have no implementation provided for virtual ~OGraph();. Either provide an implementation, specify it as pure virtual with = 0;, or just get rid of it.

The compiler is expecting to attach type info for the class to the implementation of its first virtual method. Your first virtual method is undefined, so the implementation is never generated, and you get related linking errors.

Additionally, as everyone is saying, you should remove the parentheses from your definition of myGraph.

泼猴你往哪里跑 2024-10-18 05:27:43

如果您使用“默认”构造函数构造一个新对象,我相信您在调用它时不带空括号。

因此,您将使用 OUGraph myGraph 而不是 OUGraph myGraph()

If you're constructing a new object based using a 'default' constructor, I believe you call it without the empty parenthesis.

So, you would use OUGraph myGraph not OUGraph myGraph()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文