0

I've declared a struct in the header file Player.h and I want to use it in another class but I can't get it working. I've included Player.h in the other class but it says

error C2011: 'Point' : 'struct' type redefinition.

What am I missing?

in Player.h

struct Point {  
    int x, y;
    char name[20]; 
};


class Player
{
    public:
    Player(void);
    ~Player(void);

    std::vector<Point> rocketVector;
    private:
};
Hitesh Vaghani
  • 1,342
  • 12
  • 31
thehindutimes
  • 321
  • 1
  • 6
  • 19

2 Answers2

2

You're header file needs include guards.

#ifndef MY_HEADER // or some other unique name
#define MY_HEADER
struct Point {  
    int x, y;
    char name[20]; 
};


class Player
{
    public:
    Player(void);
    ~Player(void);

    std::vector<Point> rocketVector;
    private:
};
#endif

The purpose of include guards is to prevent multiple inclusions (directly or indirectly through other headers) of a header in a translation unit. When you say #include "something" the preprocessor basically just pastes the header file at the place of directive. If that happens multiple times (like in your case), you get multiple definitions of the same class or whatever else is defined in the header.

jrok
  • 54,456
  • 9
  • 109
  • 141
0

You just have to add #pragma once at the beginning of the header file, or the famous trick :

#ifndef __PLAYER_H
#define __PLAYER_H

//content of Player.h

#endif
Michael M.
  • 2,556
  • 1
  • 16
  • 26
  • 1
    Except that `#pragma once` is implementation-dependent (even though supported by major compilers). Currently (not speaking about the hypothetical future `#once`) the standard portable way is to use include guards (what you call "the famous trick"). – gx_ Aug 22 '13 at 11:02
  • 1
    You shouldn't use [reserved names](http://stackoverflow.com/questions/228783) for the include guards. – Mike Seymour Aug 22 '13 at 11:10