Dororo
Dororo

Reputation: 3440

C++ object class problems when used in another class

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.

Upvotes: 2

Views: 1301

Answers (5)

anon
anon

Reputation:

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();

Upvotes: 4

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76531

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'.

Upvotes: 0

SoapBox
SoapBox

Reputation: 20609

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.

Upvotes: 1

Roger Pate
Roger Pate

Reputation:

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.

Upvotes: 1

Andres
Andres

Reputation: 5187

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.

Upvotes: 4

Related Questions