缺少函数体或变量Missing Function Body or Variable

除了函数原型外,编译器可以继续而不会出错,但链接器无法解析对地址的调用,因为没有保留函数代码或变量空间。With just a function prototype, the compiler can continue without error, but the linker cannot resolve a call to an address because there is no function code or variable space reserved. 在创建对链接器必须解析的函数的调用之前,您将看不到此错误。You will not see this error until you create a call to the function that the linker must resolve.

示例:对未定义的函数的调用Example: Call to an undefined function

Main 中的函数调用将导致 LNK2019,因为原型允许编译器认为函数存在。The function call in main will cause LNK2019 because the prototype allows the compiler to think the function exists. 链接器将发现它不会。The linker finds that it doesn't.

// LNK2019_MFBV.cpp
// LNK2019 expected
void DoSomething(void);
int main() {
   DoSomething();
}

示例:对已实现的函数的调用Example: Call to an implemented function

在 c + + 中,请确保包括类的特定函数实现,而不只是类定义中的原型。In C++, make sure that you include the implementation of a specific function for a class and not just a prototype in the class definition. 如果要在标头文件之外定义类,请确保在函数 () 之前包含类名 Classname::memberfunctionIf you are defining the class outside of the header file, be sure to include the class name before the function (Classname::memberfunction).

// LNK2019_MFBV_2.cpp
// LNK2019 expected
struct A {
   static void Test();
};

// Should be void A::Test() {}
void Test() {}

int main() {
   A AObject;
   AObject.Test();
}

请参阅See also

链接器工具错误 LNK2019Linker Tools Error LNK2019