0

When I make method as inline, compilation fails with this error:

Undefined symbol: 'int CPositionRequestor::State(void) const (?State@CPositionRequestor@@QBEHXZ)'

Header file:

class CPositionRequestor : public CActive
    {
    // ...

private:
    TInt iState;

public:
    inline TInt State() const;
    }

CPP file:

inline TInt CPositionRequestor::State() const
    {
    return iState;
    }
Artem
  • 517
  • 1
  • 7
  • 24
  • It is not defined inline, that doesn't help. Either drop inline or write the function body in the .h file. – Hans Passant Feb 16 '20 at 17:33
  • Such a question should come with a [mcve], which includes code and the commands used for compilation. – Ulrich Eckhardt Feb 16 '20 at 17:36
  • 2
    Inline function implementation must be visible in every translation unit it is used. So if you only use it in that cpp file it should be fine unless you try to use befor it is defined. But as you have linker error apparently you are trying to use it from different cpp file or in the same cpp file but before definition. – Slava Feb 16 '20 at 17:38

1 Answers1

3

An inline function needs it's definition to be available in the file that is calling that function.

So if You define it in one cpp file and try to call it in a second cpp file, then it will not be found.

What You need to do is move this definition into the h file. (just cut & paste it after the class definition).

Or as @einpoklum has noticed, if You don't need it, remove the inline from all the definitions.

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
  • 1
    ... or drop the `inline` if it's not necessary. – einpoklum Feb 16 '20 at 18:02
  • @robert-andrzejuk You are right, this method also used in another cpp file. Moving method\`s body to class definition resolved this problem. Thank you. – Artem Feb 16 '20 at 19:01