The following code structure:
#ifndef ARRAY_STACK_H
#define ARRAY_STACK_H
#include "Array.h"
// class ArrayStack
#endif
#include "ArrayStack.h"
// ArrayStack's methods
#ifndef ARRAY_HEADER
#define ARRAY_HEADER
#include <iostream>
// class Array
#endif
#include "Array.h"
// Array's methods
#include "ArrayStack.h"
int main() {
return 0;
}
generates these errors:
LNK1169 one or more multiply defined symbols found
LNK2005 _main already defined in Array.obj
What's the problem here? Please do note that Array.cpp
did have int main()
defined in itself when it was included in the project for the first time, but no longer has it (neither does the ArrayStack.cpp
). Also, the code compiles just fine when the int main()
in main.cpp
is omitted...
The error message means that in all the compiled code, the *.obj files, the linker finds more than one main()
function. One is obviously in main.cpp.
The first solution that comes to mind, as mentioned in comments, is to (enforce) re-compile by somehow deleting the *.obj files.
When this doesn't change anything try to rebuild your solution separately from scratch. Start with main.cpp without the include. Then successively add files where you are confident that you won't get errors. Maybe you have to comment out some lines in some cases to make compilation possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With