g++ 警告:使用但未定义内联虚函数

8
我目前遇到一个问题,关于警告信息,我无法摆脱。我的代码完全正常运行,但是这个警告一直弹出:

ChildModel.h:136:24: 警告:使用但未定义的虚函数virtual int ChildModel::getLinkCost(const Link&) const [默认情况下启用]

我在S.O上找到了this post,与我有相同的问题,但是答案针对库(定义某些内容),所以对我不起作用。

我的代码如下:

class Model {
public:
    virtual inline int getLinkCost(Link const& link) const;
};

class ChildModel: public Model {
public:
    /** Warning on the line bellow: **/
    virtual inline int getLinkCost(Link const& link) const;
};
< p > ChildModel 重新定义的唯一函数是 Model::getLinkCost,而 Model::getLinkCost 方法仅由 Model 的一个方法调用。所有方法都在 C++ 文件 Model.cpp 中定义。


如果它是"内联"的,那么链接到定义会如何工作? - chris
这里的 inline 没有任何作用。关于警告:它告诉你该函数定义。因此,请展示你的 Model.cpp(以及如何编译/链接你的代码)。 - Konrad Rudolph
5
所有的方法都被定义在一个名为“Model.cpp”的C++文件中。因此,它不应该是内联函数。内联函数必须在使用它们的每个源文件中定义;所以要么移除inline修饰符,要么将其定义在头文件中。 - Mike Seymour
内联虚函数对编译器来说很难进行内联。一般情况下我不会去使用它们。 - Neil Kirk
@MikeSeymour 好的,换句话说(只是为了确保我理解你的意思),这个工作之所以有效,是因为我在Model.cpp中定义了ChildModel :: getLinkCostModel :: getLinkCost,并且唯一使用Model :: getLinkCost的函数是Model的一个方法,也在Model.cpp中定义? - Holt
显示剩余2条评论
1个回答

21

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接