Problem with decalring a vector in a header file!!

Jun 2, 2010 at 2:24pm
Hi all,
I declared a vector of pointers in a header file named (bug.h)
1
2
3
typedef Bug* Bugpt;  //Bug is a defined class

vector <Bugpt> Bugarr(6);


But the following errors appear

1>Pred Pray Project.obj : error LNK2005: "class std::vector<class Bug *,class std::allocator<class Bug *> > Bugarr" (?Bugarr@@3V?$vector@PAVBug@@V?$allocator@PAVBug@@@std@@@std@@A) already defined in bug.obj

1>C:\Documents and Settings\Waleed\My Documents\Visual Studio 2010\Projects\Pred Pray Project\Debug\Pred Pray Project.exe : fatal error LNK1169: one or more multiply defined symbols found

Thx for ur help in advance.
Jun 2, 2010 at 2:53pm
Perhaps you're including bug.h twice or more and you forgot to use an include guard.
Jun 2, 2010 at 2:57pm
1
2
3
4
#ifndef __Bug
#define __Bug
//Your declarations here.
#endif 

^This is an include guard.

-Albatross
Jun 2, 2010 at 3:03pm
I know about the include guard and i use it.
Here is "bug.h", hope u can figure out the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef BUG_H
#define BUG_H

#include <vector>
#include "board.h"
#include "prey.h"
using namespace std;

class Bug: public Prey{
public:
	Bug(Board& boa);
	Bug(int x,int y,Board& boa);
	void breed(Board& boa);
	int getx();
	int gety();
};

typedef Bug* Bugpt;

vector <Bugpt> Bugarr(6);

void in_Bugarr(Board& boa);

#endif 
Last edited on Jun 2, 2010 at 3:06pm
Jun 2, 2010 at 3:22pm
From the linker error messages, it sounds like you defined Bugarr elsewhere too.
Jun 2, 2010 at 3:32pm
i searched for another definition for my code every where in the project more than once and I'm sure its the only one!!
Jun 2, 2010 at 3:32pm
You should not be defining Bugarr in your header file. This should only appear in a .cpp file:

 
vector <Bugpt> Bugarr(6); // definition (in cpp file) compiled only once 


Then in the header file "Bug.h" you need this:


 
extern vector <Bugpt> Bugarr; // declaration (in header file) included everywhere 

Last edited on Jun 2, 2010 at 3:32pm
Jun 2, 2010 at 3:41pm
closed account (z05DSL3A)
You should also do full scope resolution in header files not using namespace std;
Jun 2, 2010 at 3:45pm
Thank u Galik, but applying what u written changes the error message to:
error C2374: 'Bugarr' : redefinition; multiple initialization

Should I add

vector <Bugpt> Bugarr(6);

in "bug.cpp" only or in every .cpp file i include "bug.h" i it?? i tried both ways and both generate the same error.
Jun 2, 2010 at 3:49pm
Thank u Gery Wolf but your way generates the same error
error C2374: 'Bugarr' : redefinition; multiple initialization
Jun 2, 2010 at 3:54pm
closed account (z05DSL3A)
That was not an attempt to solve your problem, just pointing out that it is bad practice to use Using Declarations or Using Directives in hear files.
Jun 2, 2010 at 4:07pm
Got your point, thank u very much ;)
Jun 2, 2010 at 4:12pm
waleed 707 wrote:
in "bug.cpp" only or in every .cpp file i include "bug.h" i it?? i tried both ways and both generate the same error.


Only in bug.cpp , that should solve your problem.

Make sure you do not have that statement in any other file and that you do NOT #include your bug.cpp file anywhere. Only the bug.h file should be #included.
Jun 2, 2010 at 4:59pm
Tried this too, but the same error keeps appearing!!
Topic archived. No new replies allowed.