another unknown error

Well I got an error earlier today and I've been trying to solve it the whole day already.. But I don't understand what the error means and I can't find any syntax problems:
1>...\player.h(2) : error C2011: 'Objects' : 'class' type redefinition
1> ...\player.h(2) : see declaration of 'Objects'


1
2
3
4
5
6
7
8
9
10
11
class Objects
{
public:
    Objects(char c=' ')
    {
        mychar = c;
    }
    char draw();
protected:
    char mychar;
};


Now what did I do wrong?
Do you have the #ifdefs around the class?

1
2
3
4
5
6
#ifndef OBJECTS_H
#define OBJECTS_H

// Class definition goes here

#endif 


Stops multi-definitions.
Uhm you lost me there: I didn't really look into the macros yet. So I need to put this around everything to make sure a librarie isn't declared multiple times?
You need to use this technique in header files. This is because multiple source files in one program can include the same header effectively defining the same class multiple times in one program, i.e. each time the header is included.

put lines 1 and 2 of Zaita's post before any code in your header files and line 6 as the last line of the header. don't use the same define name more than once, the usual convention is to tie the define name to the file name.

e.g.
1
2
3
4
5
6
7
//file header1
#ifndef HEADER1_H
#define HEADER1_H

. . .

#endif 
1
2
3
4
5
6
7
//file header2
#ifndef HEADER2_H
#define HEADER2_H

. . .

#endif 
Exactly. And those are called "inclusion guards", not "macros". Macros are something different.
Thanks for the help. Makes you think why the compiler doesn't automatically only include every file 1 time (what's the point of including the same thing twice)
Anyway, working a bit more yields yet another error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef OBJECTS_H
#define OBJECTS_H
#include "field.h"
class Objects
{
public:
    int x,y;
    Objects(int xx, int yy,field* f, char c=' ')
    {
        x=xx;
        y=yy;
        mychar = c;
        myfield = f;
    }
    char draw();
    void set(int xx,int yy)
    {
        x = xx;
        y = yy;
    }

protected:
    char mychar;
    field *myfield;
};
#endif 


Yields with an error player.h(10) : error C2061: syntax error : identifier 'field'
What does this mean lol? Doesn't this happen if "field" isn't declared already? - Yet I know it is (and I know it's working) - cause in the main code I have this:
1
2
3
        playfield = new field(ww,hh);
        mainplayer = new Object(1,1,playfield,'O');
        playfield->set(mainplayer,mainplayer->x,mainplayer->y,playfield);
Which DOES work, if I leave out the third argument it works....

Mostly because it's not the compiler that does the inclusion, but the preprocessor, which is not known for its smartness (not that it should be any other way).
How about posting the code that produces the error?
Uhm that is the the above code! (above is "player.h" - the problem is in the first line of the constructor).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef FIELD_H
#define FIELD_H
#include <string>
#include <iostream>
#include "player.h"
void clearscreen();
class field
{
public:
    field(int width, int height,int offset = 0, char filler = ' ')
    {
        w=width;
        h=height;
        o=offset;
        //fieldarr[w][h]='*';
        fieldarr = new Objects**[w];
        for (int i=0;i<w;i++)
        {
            fieldarr[i]=new Objects*[h];
            for (int j = 0; j<h;j++)
            {
                fieldarr[i][j] = 0;
            }
        }
    }
    field(const field &f)
    {
        h=f.h;
        w=f.w;
        o=f.o;
        fieldarr = new Objects**[w];
        for (int i=0;i<w;i++)
        {
            fieldarr[i]=new Objects*[h];
            for (int j = 0; j<h;j++)
            {
                fieldarr[i][j] = f.fieldarr[i][j];
            }
        }
    }
    ~field()
    {
        for (int i=0;i<w;i++)
        {
            delete[] fieldarr[i];
        }
        delete[] fieldarr;
    }
    field &operator=(const field &f);

    void drawfield();
    void set(Objects *v, int x, int y);
    Objects* get(int x, int y);
private:
    int w,h,o;
    Objects ***fieldarr;

};

^^ that's my "field.h" in which field is declared (field should be a 2 dimensional array filled with "pointers to 'Objects')..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <ctime>
#include <ctime>
#include <iostream>
#include "field.h"
#include "player.h"

class game
{
public: 
    game(int ww,int hh, double ss=1)
    {
        playfield = new field(ww,hh);
        mainplayer = new Object(1,1,playfield,'O');
        playfield->set(mainplayer,mainplayer->x,mainplayer->y,playfield);
        w = ww;
        h = hh;
        step = CLOCKS_PER_SEC/ss;
    }
    game(const game &g)
    {
        copygame(g);
    }
    game &operator=(const game &g);
    void copygame(const game &g);
    void deletegame();
    ~game ()
    {
        deletegame();
    }
    clock_t gamehandle();


private:
    field *playfield;
    player *mainplayer;
    int w,h;
    clock_t step;
};


And that is "game.h" which is the header file that "calls"/"creates" those fields and objects
Assuming that your field.h is the complete file, you haven't got an #endif at the bottom of the file. This is going to give you weird problems (which is what you have)
Topic archived. No new replies allowed.