Home > Net >  VS 2022: "Unresolved external" error when calling methods that are defined outside of the
VS 2022: "Unresolved external" error when calling methods that are defined outside of the

Time:04-21

So, I've tried to run the simplest C program imaginable in Visual Studio 2022:

main.cpp:

#include "TestClass.h"

int main() {
    TestClass().testMethod();
}

TestClass.h:

#pragma once
class TestClass {
public:
    void testMethod();
};

TestClass.cpp:

#include "TestClass.h"

inline void TestClass::testMethod() {

}

But for some reason, I get nothing but a linker error:

error LNK2019: unresolved external symbol "public: void __cdecl TestClass::testMethod(void)" (?testMethod@TestClass@@QEAAXXZ) referenced in function main

I know that there are tons of questions on Stack Overflow discussing that specific error, but I wasn't able to find anything that applied to my situation, except for this one, which doesn't have an answer.

All files are included in the project (everything was generated in Visual Studio), I do not get any warnings from IntelliSense, and every file on its own compiles just fine (using Ctrl F7)

I have no clue what is going on and would appreciate any help.

CodePudding user response:

In C inline functions must have their body present in every translation unit from which they are called.

Removing inline doesn't change anything

Try to test it. I'm not the only one who proves that inline causes the lnk error. As Sedenion says, it's a usage error.

I recommend reporting this wrong behavior to Developer Community and posting the link in this thread.

  • Related