Hi all,
I declared a vector of pointers in a header file named "bug.h"

#ifndef BUG_H
#define BUG_H

#include <vector>
#include "board.h"
#include "prey.h"

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;

std::vector <Bugpt> Bugarr(6);

void in_Bugarr(Board& boa);

#endif

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

I tried to use extern too

extern vector <Bugpt> Bugarr(6);   //in "bug.cpp"

But this error appeared

error C2374: 'Bugarr' : redefinition; multiple initialization

Thx for ur help in advance.

Recommended Answers

All 6 Replies

Why is Bugarr defined in that header? If it is not part of the class (which it is not, it is a global variable), then it should probably not belong in the class's header.

Why is Bugarr defined in that header? If it is not part of the class (which it is not, it is a global variable), then it should probably not belong in the class's header.

Its used in the definition of these functions so it should be declared for them.
Do u have any other ideas rather than declaring it here?

Why not make it a member variable if it is being accessed by member functions? Does it need to be accessed outside of the class? If so you can provide an accessor function or make it public.

Dave

Well, its accessed outside its class, isn't there any other way, coz making it a member variable will make me edit a 1200 lines of code that is to be submitted tomorrow :S

The problem is that you're trying to initialize the vector in the header file. Keep it extern, but declare it in the header file like extern vector<Bugpt> Bugarr; . Then, in a source file, define and initialize it like vector<Bugpt> Bugarr(6); I think that should work.

Thank u Ariste u are my HERO. I've been searching for hours for a solution.
Thank u very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.