0

I have seen that this question has been asked but not exactly from this approach. I looked at the other threads and did not see a solution for approach I have to take on this project. Please forgive me for the lengthy post in advance. I have to solve this issue from an OOP perspective, thing is I am trying to use an array to hold the number of disks that the user enters. I do not see another way of doing it.

            **This is my .h file for the class Tower.**
            #include <iostream>
            #include <string>
            #include "Location.h"

            using namespace std;
            //Class for the tower
            class Tower
            {
            private:
                Location _location; 

                public:
                    //Constructor to build object
                    Tower(Location location);
                    //set get functions to set and get values.
                    void setLocation (Location newLocation);
                    Location getLocation()const;  

                };

        **This is my location.h file i used an enum datatype to specify the location of each peg.**

            enum Location
            {
                start,
                middle,
                last
            };
        **My disk.h file that has the properties of my disks.**
        using namespace std;

        class Disk
        {
        private:
            int _diskLocation;
            int _diskSize;

        public:
            Disk(Tower tow1, int sizeOfDisk);
            void setdiskSize(int);
            int getdiskSize()const;
            void setdiskLocation(Tower newTower);
            Tower getdiskLocation()const;

        };
    **This is my implementation file where I initialized the constructor and its data members.**
    using namespace std;

    Tower::Tower(Location location)
    {
        setLocation(location);
    }

    Disk::Disk(Tower tow1, int sizeOfDisk)
    {
        setdiskSize(sizeOfDisk);
        setdiskLocation(tow1);
    }

Finally the main.cpp file. here i  make the objects to be used but when i try to pass in the number of disks entered by the user into the array i get the following error. **"Array initializer must be an initializer list"**
int main(int argc, const char * argv[])
{

    Tower tower1(start);
    Tower tower2(middle);
    Tower tower3(last);


    int numberOfDisks =0;

    cout << "Enter number of disks: " << endl;
    cin >> numberOfDisks;


     Disk disks[] = new Disk [numberOfDisks]   
    return 0;
}

I am beginning to think the problem is unsolvable using this approach. Every solution for this problem that I have found on the internet has it being done procedurally. Barring some other languages and programming concepts I have not been taught yet. This is my second programming class I have ever taken. Thank you for the help.

1 Answers1

0

There's a difference between Disk disks[] and Disk* disks. You can do Disk* disks = new Disk[numberOfDisks]; (note the ; which your example is missing) but you can not do Disk disks[] = new Disk[numberOfDisks];. See this post to understand why.

Community
  • 1
  • 1
Oliver Dain
  • 9,617
  • 3
  • 35
  • 48