无法使函数公开。获取:"LNK2005"错误。如何调试链接器错误

Cannot make function public. Getting: "LNK2005" error. How to debug linker errors

本文关键字:错误 何调试 调试 链接 获取 LNK2005 函数      更新时间:2023-10-16

在我的项目中,MyFourierClass::forward_fft只有一个定义。当我将 MyFourierClass::forward_fft 声明为公共时,我收到此错误,否则没有错误。

错误信息:

1>my_fourier.obj : error LNK2005: "public: void __cdecl MyFourierClass::forward_fft(int)" (?forward_fft@MyFourierClass@@QEAAXH@Z) already defined in main.obj
1>CGPProjectx64DebugCGPProject.exe : fatal error LNK1169: one or more multiply defined symbols found

my_fourier.cpp:

#ifndef MY_FOURIER
#define MY_FOURIER
class MyFourierClass {
double** dataset = 0; 
//public: // <-- un-commenting this line causes the linker error.
void forward_fft(int);
};
void MyFourierClass::forward_fft(int bins) {
bins = bins + 1;
};
#endif

主要:

#ifndef MY_MAIN
#define MY_MAIN
#include "my_fourier.cpp"
int main() {
int i = 0;
}
#endif

是否有调试链接器错误的标准方法?我认为另一个文件中可能有定义,所以我删除了项目中的所有其他文件。现在只有主.cpp和my_fourier.cpp。我正在使用Visual Studio 2019。

提前谢谢。

当您使用 include 指令将模块my_fourier.cpp包含在模块中时

#include "my_fourier.cpp"

那么这个函数

void MyFourierClass::forward_fft(int bins) {
bins = bins + 1;
};

至少定义两次。

您应该将类定义放在标头中,并且此标头包含在模块中 my_fourier.cpp定义成员函数的位置,并在模块中,主模块从最后一个模块中删除指令

#include "my_fourier.cpp"
相关文章: