tags:

views:

142

answers:

5

Having trouble when trying to create a class using another class(and 2 inner classes), I think it might be a syntax problem.

The first class

class listitem
{
//listitem.h(11)
public:
    //MONSTER CLASS
    static class monster
    {
    public:
        monster(string thename); 
        monster(void);
        ~monster(void);
    private:
        string name;
        int level;
    };
    //PLAYER CLASS
    static class player
    {
    public:
        player(string _pname, int _plevel, int _maxhp, int _currhp);
        player(void);
        ~player(void);
    private:
        string pname;
        int plevel;
        int maxhp;
        int currhp;
    };
public:
    listitem(player member, monster head);
    ~listitem(void);
private:
    player a;
    monster b;
    //other fields
};

The second class is where I encounter a problem:

class hatelist
{
private:
    vector<listitem> thelist;
public:
    hatelist(listitem newlist);
    int addNewListItem(listitem item);
    hatelist(void);
    ~hatelist(void);
};

The implementation of the offending code:

hatelist::hatelist(listitem inputlist)
{ //hatelist.cpp(6)
    thelist.push_back(inputlist);
}
1>hatelist.cpp
1>c:\...\listitem.h(11) : error C2011: 'listitem' : 'class' type redefinition
1>c:\...\listitem.h(11) : see declaration of 'listitem'
1>c:\...\hatelist.cpp(6) : error C2027: use of undefined type 'listitem'
1>c:\...\listitem.h(11) : see declaration of 'listitem'
1>c:\...\hatelist.cpp(6) : error C2227: left of '->{dtor}' must point to class/struct/union/generic type

Any help would be appreciated.

+3  A: 

Did you protect your header with a

#ifndef LISTITEM_H
#define LISTITEM_H
 // All of your code

#endif

If not, it could be getting included twice, causing your error.

Andres
Bingo, that was the problem, thanks very much guys.
Dororo
No problem. Btw, Neil's points about monster(void) and static are probably good to note.
Andres
+4  A: 

C++ has no concept of "static classes". The construct:

static class A { ... } a;

is allowed - the object 'a' is static. But usages like:

static class A { ... };

should really be syntax errors.

Also, but nothing to do with your problem, saying things like:

monster(void);

is unidiomatic C++ - the commonly preferred idiom is to omit the 'void' (the two are semantically identical):

monster();
anon
+1  A: 

You cannot apply static to classes, remove that and I don't see any other syntax errors. Make sure you use an include guard in all of your headers.

Roger Pate
A: 

It's difficult tot ell from the code you've pasted because it doesn't seem to be the entire file. Based on the error message, it sounds like you forgot to 'guard' your header file:

#ifndef __MYHEADER_H_
#define __MYHEADER_H_

// header code goes here

#endif

And of course make sure you include the header in your .cpp file.

SoapBox
But don't use reserved identifiers for your include guards.
Roger Pate
Names that include double underscores or that begin with an underscore and an uppercase letter are reserved for the C++ implementation - you are not allowed to create such names in your own code.
anon
A: 

I'm guessing that you don't have include guards in listitem.h and you are including the file multiple times.

An include guard allows you to include a file multiple times without problems. The first time you include the file, you get the actual defintions/declarations. After that, the include file 'turns blank'.

R Samuel Klatchko