ozdrgnaDiies
ozdrgnaDiies

Reputation: 1929

LNK2001 Only some inline functions unresolved?

This has been a problem for some time now but I haven't found any real solution. Basically, I have several classes that have roughly 10-20 inline functions each that just retrieve simple information from the class. All of them are one line functions and all of them do the same thing with different parameters. Most of the time they compile fine, but every once in awhile the compiler will say something is unresolved in the middle of functions with the same sort of definition.

For example, here's a part of my class' header definition which has public scope:

...
inline uint32_t debug_offset();
inline uint32_t debug_load_addr();

inline uint32_t dol_offset();
inline uint32_t fst_offset();
inline uint32_t fst_size();
inline uint32_t fst_max_size();
...

And here are their definitions:

...
uint32_t Header::debug_offset()
{
    return VectorData::read<uint32_t>(ref(header), Offset::DebugOffset);
}

uint32_t Header::debug_load_addr()
{
    return VectorData::read<uint32_t>(ref(header), Offset::DebugAddress);
}

uint32_t Header::dol_offset()
{
    return VectorData::read<uint32_t>(ref(header), Offset::DOLOffset);
}

uint32_t Header::fst_offset()
{
    return VectorData::read<uint32_t>(ref(header), Offset::FSTOffset);
}

uint32_t Header::fst_size()
{
    return VectorData::read<uint32_t>(ref(header), Offset::FSTSize);
}

uint32_t Header::fst_max_size()
{
    return VectorData::read<uint32_t>(ref(header), Offset::FSTMaxSize);
}
...

All of these will compile fine along with the other 20 or so similar functions, except for fst_offset which throws this error:

Error   1   error LNK2001: unresolved external symbol "public: unsigned int __thiscall Header::fst_offset(void)" (?fst_offset@Header@@QAEIXZ)

This has happened to several other classes as well. I'll have one or two out of all the inline functions throw this error when all the others are fine. The simple fix is to just take the functions that throw the error and put the body in the header, but that changes the layout of the project and makes it not consistent.

Is this a compiler error? Am I doing something different and I don't know it? I want to avoid defining some functions in headers and others in source files if at all possible.

Upvotes: 0

Views: 681

Answers (2)

ozdrgnaDiies
ozdrgnaDiies

Reputation: 1929

I want to answer this fully than just accept a really basic answer based on what I've found.

My original project wanted to stay away from having a mix of function prototypes and function declarations inside a single class definition. To do this I tried to define inline functions in the cpp file like you would normal functions which obviously doesn't work.

As a compromise, I found that you can define a function in a header then outside the class definition you can put the function declarations as if you would in the cpp file. This is probably what I was thinking of when I put the inline functions in the cpp file.

Example:

// example.h

class Example
{
public:
    void foo();
    void bar(); // Not defined in header, has to be in cpp file and non-inline
}

// Outside Example class definition.
inline void Example::foo()
{
    // Code
}

Upvotes: 2

Callum
Callum

Reputation: 25

From my understanding, if you're using inline, you got to define the function in the header file and not just declare it.

Upvotes: 1

Related Questions