2

there is only one definition of MyFourierClass::forward_fft in my project. When I declare MyFourierClass::forward_fft as public, I get this error, otherwise there is no error.

Error Message:

1>my_fourier.obj : error LNK2005: "public: void __cdecl MyFourierClass::forward_fft(int)" (?forward_fft@MyFourierClass@@QEAAXH@Z) already defined in main.obj
1>CGPProject\x64\Debug\CGPProject.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

Main:

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

}
#endif

is there a standard method for debugging linker errors? I thought there might be a definition in another file, so I've delete all other files in my project. There is now only main.cpp and my_fourier.cpp. I'm using Visual Studio 2019.

Thanks in advance.

Xephex
  • 33
  • 5
  • 2
    Header file for the class definition. Source file for the function definition. And `#include` the *header* file, not source files. – Some programmer dude Jun 24 '20 at 19:33
  • 3
    You should split `my_fourier.cpp` into `.cpp` and `.h` and include the latter. – rustyx Jun 24 '20 at 19:35
  • Thank you! I always thought splitting into .h and .cpp was optional, it works fine now. Thanks a lot! If someone answers the question I'll mark it as solution. – Xephex Jun 24 '20 at 19:38
  • Does this answer your question? [error LNK2005, already defined?](https://stackoverflow.com/questions/10046485/error-lnk2005-already-defined) – Ken White Jun 24 '20 at 19:43
  • 1
    @Xephex it is optional, you can `#include` anything, but more often than not, circumstances force you to go with the tried-and-true headers and implementation files approach. Any time you have functions and data definitions included by multiple files (whether the definitions are in a .h or a cpp file) you're going to compile, or a smart build system that sees the cpp file and automatically compiles and links it, you will have multiple definitions, and the linker hates that. – user4581301 Jun 24 '20 at 20:22

1 Answers1

1

As you included the module my_fourier.cpp in the module with main using the include directive

#include "my_fourier.cpp"

then this function

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

};

is defined at least twice.

You should place the class definition in a header and this header include in the module my_fourier.cpp where the member function is defined and in the module with main removing from the last module the directive

#include "my_fourier.cpp"
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Alternatively, move the definition of `forward_fft` into the class definition. Then it becomes implicitly `inline`, which means it's OK to define it multiple times, as long as each definition is identical. Or, just declare it `inline` yourself. These solutions are not as usual as separating declaration from definition, as in this answer. – HTNW Jun 24 '20 at 20:37