Multiple definition error using Dev-C++

Jun 23, 2015 at 5:24am
Hello, I am attempting to code a stack structure in C++ using the Dev-C++ compiler. I currently have 3 files: main.cpp, DataStructures.h, and DataStructures.cpp. Every time I attempt to compile it, I get this error: DataStructures.cpp:(.bss+0x0): multiple definition of `node'. I have looked online and tried using extern, adding header guards, editing the makefile, etc. to no avail.

main.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "DataStructures.h"

using namespace std;

int main() {
	Stack theStack;
	cout << theStack.myNode->val;
	return 0;
}


DataStructures.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef DATASTRUCTURES_H
#define DATASTRUCTURES_H

using namespace std;

struct Node {
	int val;
	Node* next;
} node;
typedef Node* nodeptr;

class Stack {
	public:
		Stack();
		~Stack();
		Node* myNode;
	private:
		
};
#endif 


DataStructures.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "DataStructures.h"

using namespace std;

Stack::Stack() {
	myNode = new Node;
	myNode->val = 10;
	myNode->next = NULL;
}

Stack::~Stack() {
	delete myNode;
}


Makefile.win
1
2
3
4
5
6
7
8
*constants*
*all command*
*clean command*
DataStructures/main.o: DataStructures/main.cpp
	$(CPP) -c DataStructures/main.cpp -o DataStructures/main.o $(CXXFLAGS)

DataStructures/DataStructures.o: DataStructures/DataStructures.cpp
	$(CPP) -c DataStructures/DataStructures.cpp -o DataStructures/DataStructures.o $(CXXFLAGS)

[/code]
Jun 23, 2015 at 7:30am
http://www.cplusplus.com/forum/general/140198/ (see global variables)
1
2
3
4
5
6
7
8
9
#ifndef DATASTRUCTURES_H
#define DATASTRUCTURES_H

using namespace std; //don't using on headers, all that includes it will have it.

struct Node {
	int val;
	Node* next;
} node; //<-- here 


¿do you really want `node' to be a global variable? you don't even use it.
Jun 23, 2015 at 3:36pm
Ok I've fixed it like you said but it's still giving me the same error
Jun 23, 2015 at 3:56pm
Did you fix both the issues ne555 pointed out?
Jun 23, 2015 at 4:06pm
in your linker parameters, write the following command-line parameter:
1
2
-Wl,--allow-multiple-definition
-Wl,--enable-runtime-pseudo-reloc
Jun 23, 2015 at 4:41pm
thank you heepoo it worked. But is it bad that there exists a multiple definition at all?
Last edited on Jun 23, 2015 at 4:41pm
Jun 23, 2015 at 4:44pm
no, the linker resolve's everything itself
Topic archived. No new replies allowed.