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
.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With