1

I'm trying to create a class that contains structs of the following types:

 struct gameObject
       {
         int row;                       // row position of the object
         int column;                    // column position of the object
         bool isFound;          // flag to indicate if the object has been found (or is dead, in the case of the monster)
         bool isVisible;        // flag to indicate if the object can be seen on the board -add in week 4
        };

struct playerObject
{
    bool alive;             // flag to indicate if the player is alive or dead
    bool hasWeapon;         // flag to indicate if the player has the weapon
    bool hasTreasure;       // flag to indicate if the player has the treasure
    bool hasTorch;          // flag to indicate if the player has the torch -add in week 4
    bool hasNoisemaker;     // flag to indicate if the player has the noisemaker -add in week 4
    bool hasKey;
    gameObject position;    // variables for row, column and visibility
};

I'm doing this so I can have all of my "game pieces" and their data under one object to hopefully help with readability. The class I'm trying to declare is below:

class pieces{//the class for the game and player objects
public:
    pieces();
    ~pieces();
    gameObjectType hold = EMPTY;                         // Holds objects under the monster<bug fix>
    // </WK6>
    playerObject player = { true, false, false, false, false, false, { -1, -1, false, true } };  // the player
    gameObject treasure ={ -1, -1, false, true };           // the treasure
    gameObject monster = { -1, -1, false, true };           // the monster
    gameObject weapon = { -1, -1, false, true };            // the weapon
    gameObject torch = { -1, -1, false, true };             // the torch
    gameObject noisemaker = { -1, -1, false, true };  // the noisemaker
    gameObject key = { -1, -1, false, true };
    gameObject caveExit = { -1, -1, false, true };          // the cave exit

};

The problem is that I keep getting a C2661 error. For all of my gameObject declarations it says error C2661: 'gameObject::gameObject' : no overloaded function takes 4 arguments However, for my playerObject, it says the same except with 7 arguments instead of 4.

I've been at it for hours and I can't figure out what's going on.

Swapnil
  • 1,424
  • 2
  • 19
  • 30

1 Answers1

-1

If you declare you structs like this:

typedef struct gameObject {
   {
     int row;                       // row position of the object
     int column;                    // column position of the object
     bool isFound;          // flag to indicate if the object has been found (or is dead, in the case of the monster)
     bool isVisible;        // flag to indicate if the object can be seen on the board -add in week 4
    } gameObject;

Then I expect it will all work. Note the gameObject after the '}'

Paul Coldrey
  • 1,389
  • 11
  • 20
  • Interestingly in VS2013 I get this when I run your code: error C2797: 'pieces::player': list initialization inside member initializer list or non-static data member initializer is not implemented – Paul Coldrey Dec 16 '16 at 03:53
  • seems @birryee is correct: https://blogs.msdn.microsoft.com/somasegar/2013/06/28/c-conformance-roadmap/ – Paul Coldrey Dec 16 '16 at 03:57
  • This is C++, not C. Nobody would write such code in C++ except maybe if he share some code between C and C++. – Phil1970 Dec 16 '16 at 04:10
  • I'm still learning c++, so my understanding of it is shaky at best. Could you elaborate? – Karla Barraza Dec 16 '16 at 04:22
  • 1
    @KarlaBarraza [In C, user-defined type names are put in the "tag" name space, and need to be preceded by `struct`/`enum`/`union`](http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c/612350#612350); you have to explicitly `typedef` the type if you want to use it as is. For example, in your code, you would have to use `struct gameObject` to declare a `gameObject`, instead of just `gameObject`. `typedef struct X X;`, as shown here, lets you use the name by itself, as `gameObject`. ...In C++, this is unnecessary, and "tag names" are available as is. – Justin Time - Reinstate Monica Dec 16 '16 at 04:50
  • @PaulColdrey The issue appears to be that the compiler has trouble with default member initialisers, when the member being initialised is of a user-defined type. Providing the name as a `typedef` wouldn't solve it; if it _does_, that means it's probably time to get a new compiler. – Justin Time - Reinstate Monica Dec 16 '16 at 05:03
  • @Justin Time - Agreed as per my later comment. Also, sadly, the reality is he does need a new compiler because it is a failing of VS 2013.On reflection I agree this is not very C++-like but the reality is that his code is correct and the compiler is crap so it was not that unlikely that this more prescriptive (old-school) syntax might have solved his problems. – Paul Coldrey Dec 19 '16 at 05:49