开发者

Problems with classes and types

I have a class that I'm trying to use as a type

class ship{
    public:
        vector <int> xp;
        vector <int> yp;
        vector <bool> pos;
        shipType vessel;
        bool active;
    ship(){}
    void randommethods(){
        blah;
    }
};

I want to include in a fleet class an array of type ship.

class fleet{
    public:
        ship boat[7];
    boat(){}
};

I've included the a header file for ship.h in the source file for fleet but it 开发者_StackOverflowdoesn't recognize ship as a type.

fleet.cpp:12: error: ISO C++ forbids declaration of ‘boat’ with no type
fleet.cpp:13: error: declaration of ‘int fleet::boat()’
fleet.cpp:11: error: conflicts with previous declaration ‘ship fleet::boat [7]’

I can use a similar declaration to work with a variable of type ship and all of the member methods within ship.cpp. What am I doing wrong any help would be appreciated.


In your code for fleet class you declare an array of ship( shop boat[7] ) named boat AND a function member returning int ( boat(){} ) named boat. You try to declare two distinct entities with the same name in one namespace and this is error. That what compiler tries to tell you.

I guess you wanted to wright a default constructor for fleet class smth like this:

class fleet{
    public:
        ship boat[7];
    fleet(){} //fleet, not boat
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜