Problem with decalring a vector in a header file!!

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.
Perhaps you're including bug.h twice or more and you forgot to use an include guard.
1
2
3
4
#ifndef __Bug
#define __Bug
//Your declarations here.
#endif 

^This is an include guard.

-Albatross
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
From the linker error messages, it sounds like you defined Bugarr elsewhere too.
i searched for another definition for my code every where in the project more than once and I'm sure its the only one!!
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
closed account (z05DSL3A)
You should also do full scope resolution in header files not using namespace std;
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.
Thank u Gery Wolf but your way generates the same error
error C2374: 'Bugarr' : redefinition; multiple initialization
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.
Got your point, thank u very much ;)
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.
Tried this too, but the same error keeps appearing!!
Topic archived. No new replies allowed.