C++ 为什么调用在另一个文件中声明和定义的成员函数会导致链接错误?
我正在尝试调用公共成员函数:C++ 为什么调用在另一个文件中声明和定义的成员函数会导致链接错误?,c++,linker,linker-errors,lnk2019,C++,Linker,Linker Errors,Lnk2019,我正在尝试调用公共成员函数: // main.cpp #include "AH.h" int somefunc() { //... // it - object of class A it.set_mean(angle, mean); //... } 在.h.中声明的 // AH.h class A { //... public: inline void set_mean(const int angle, const double mean_value); //... } 并在
// main.cpp
#include "AH.h"
int somefunc()
{
//...
// it - object of class A
it.set_mean(angle, mean);
//...
}
在.h.中声明的
// AH.h
class A
{
//...
public:
inline void set_mean(const int angle, const double mean_value);
//...
}
并在.cpp文件中定义:
// A.cpp
#include "AH.h"
inline void A::set_mean(const int angle, const double mean_value)
{
mean[angle] = mean_value;
}
以及接收LNK错误:
error LNK2019: unresolved external symbol "public: void __cdecl A::set_mean(int,double)" (?set_mean@A@@QEAAXHN@Z) referenced in function "int __cdecl somefunc(class std::vector<class A,class std::allocator<class A> > &)" (?somefunc@@YAHAEAV?$vector@VA@@V?$allocator@VA@@@std@@@std@@@Z)
为什么会发生这种情况?如果编译器看不到实现,它如何内联函数?你确定你正在链接a.o吗?你是如何编译代码的?@David,是的,没错!谢谢在.h文件中添加了定义,现在可以使用了。