-2

I have the following code:

IFile.h

class IFile { public: IFile(); ~IFile(void); inline bool IsValidFileType() const; }; 

IFile.cpp

IFile::IFile() { //IsValidFileType(); } IFile::~IFile(void) { } inline bool IFile::IsValidFileType() const { return true; } 

main.cpp

int main(int argc, char* argv[]) { IFile* pFile = new IFile(); pFile->IsValidFileType(); return 0; } 

When compiling the code I get the following error: error LNK2019: unresolved external symbol "public: bool __thiscall IFile::IsValidFileType(void)const " (?IsValidFileType@IFile@@QBE_NXZ) referenced in function _main

If I change wither "inline" or "const" qualiferes for the function, or call it inside the constructor, the program will complile. Can you please explain this behaviour?

3
  • 2
    For it to be inlined, it needs to be visible at the call location. Putting it in the header would be one way to fix it Commented Mar 1, 2013 at 14:43
  • 1
    "inline" doesn't mean "not in line" :-( Commented Mar 1, 2013 at 14:43
  • Please search before asking. There are so many duplicates for this.... Commented Mar 1, 2013 at 14:45

4 Answers 4

0

How can the compiler inline a function whose code it cannot see while it is compiling? When compiling main.cpp, the compiler is being asked to do just this.

Sign up to request clarification or add additional context in comments.

Comments

0

An inline function's code gets compiled into each translation unit that references it (that's the idea, after all). Meaning, you need to include the code in the header file.

Comments

0

The inline keyword promises to the compiler that it will be able to see the definition in each translation unit (*.cpp file) in which it is used. You break this promise, since main.cpp can't see the definition although it includes IFile.h.

Usually functions with the inline keyword should be defined in a header file, not a source file.

2 Comments

Thank you. That makes sense, but why removing the const also solves the issue?
I can't see any reason removing the consts and doing nothing else would make a difference here.
0

Since the function is inline, you have to define it in the header file, not in the cpp file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.