继承问题
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
2BUILD FAILED (exit value 2, total
time: 568ms)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是:
声明一个名为
myGraph
的函数,该函数返回一个OUGraph
。您需要改为:这按照您的意图声明了一个变量。
This:
is declaring a function called
myGraph
that returns aOUGraph
. You need to instead have:This declares a variable, as you intend.
这是所有 C++ 新人都会迷恋的老东西。
您尚未创建名为 myGraph 的变量,而是声明了一个名为 myGraph 的函数,该函数返回 OUGraph。去掉“()”。
您显然也从未在任何地方定义过您的基地的析构函数。即使它是空的,您也需要定义它,因为您在类定义中声明了它。
It's the same old thing all new C++ people fall for.
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.
您没有为
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
.如果您使用“默认”构造函数构造一个新对象,我相信您在调用它时不带空括号。
因此,您将使用
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
notOUGraph myGraph()