Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ warnings, inline virtual function used but not defined

Tags:

c++

g++

I'm currently having a problem with a g++ warnings I cannot get rid off. My code is working perfectly but this warning keeps poping up:

ChildModel.h:136:24: warning: inline function virtual int ChildModel::getLinkCost(const Link&) const used but never defined [enabled by default]

I currently found this post on S.O, with the same problem, but the answer is specific to the library (defining something) so it doesn't work for me.

My code is as follow:

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;
};

The only function redefined by ChildModel is Model::getLinkCost, and the Model::getLinkCost method is only called by a method of Model. All the method are defined in a C++ file Model.cpp.

like image 268
Holt Avatar asked Jul 08 '14 15:07

Holt


People also ask

Can you inline a virtual function?

Whenever a virtual function is called using a base class reference or pointer it cannot be inlined because the call is resolved at runtime, but whenever called using the object (without reference or pointer) of that class, can be inlined because the compiler knows the exact class of the object at compile time.

What is the function of the __ inline?

The __inline keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not __inline is honored depends on the setting of the inline optimizer option. By default, the inline option is in effect whenever the optimizer is run.

What should we not use while making a function inline?

We should not use functions that are I/O bound as inline functions. When large code is used in some function, then we should avoid the inline. When recursion is used, inline function may not work properly.


1 Answers

From http://en.cppreference.com/w/cpp/language/inline

The definition of an inline function must be present in the translation unit where it is called.

If you are going to define the function in a .cpp file, you must remove the inline specifier from the .h file.

like image 167
R Sahu Avatar answered Sep 28 '22 13:09

R Sahu