Thread: An Array of Classes in a different Class

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    An Array of Classes in a different Class

    What I am trying to do below in make an array of GridSpot objects in my Solve class. I don't really know what the issue is here because I feel like I should be able to make an array of objects. The array is even empty now, so I dont know what to make of it.

    GridSpot Class:

    Code:
     class GridSpot{
          public:
          GridSpot(int zero, int one, int two, int three, string name);
          int getSpot(int spot) const {return gridNumbers[spot]; }
          int setOrientation(int newOrien) {orientation = newOrien;}
          void rotateGrid(int rot);
          string getName() {return cardName;}
          
          private:
          int gridNumbers[4];
          int orientation;
          string cardName;
          };
    Solve Class:

    Code:
     class Solve{
          public:
          Solve(GridSpot toBeSolve[]);
          void printSolve(GridSpot printMe[]);
          void solveThisStuff(GridSpot finalArray[]);
          void switchCards(int spotToBeSwitched);
          
          private:
          GridSpot gridToBeSolved[9];
          };


    errors:

    Code:
     
    - X:\csci241\mp3.cpp In constructor `Solve::Solve(GridSpot*)':
    -92 X:\blenkhor\csci241\mp3.cpp no matching function for call to `GridSpot::GridSpot()' 
    -  note X:\blenkhor\csci241\mp3.cpp:11 candidates are: GridSpot::GridSpot(const GridSpot&) 
    - note X:\blenkhor\csci241\mp3.cpp:11                 GridSpot::GridSpot(int, int, int, int, std::string)
    these errors point to my constructor of my Solve class, I don't have the constructor doing anything yet, I just need to get this bog worked out

    Constructor for Solve:
    Code:
          Solve::Solve(GridSpot toBeSolved[]){      
                                         
          }
    Anyone have any ideas because this is frustrating me to no end?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The error means you need to define a default constructor for your GridSpot class. In particular, you would need one because in order for your Solve constructor to build an object of type Solve, it must be able to construct all subobjects it has. This includes the array of 9 GridSpot objects that the Solve object declares as a member. The array requires a default constructor for all the objects to get created which it does not have... therefore, the Solve constructor cannot build a Solve object since it cannot build its own subobjects. (I think...)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    So, do you have any suggestions on what I should do then?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    "The error means you need to define a default constructor for your GridSpot class."
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    ah sorry, thank you for the help it worked great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 02:20 AM
  2. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 11:43 AM
  3. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM