0
我已经看到,这个问题已被问到,但不完全从这种方法。我查看了其他线程,并没有看到我必须采取的方法的解决方案。请原谅我冗长的帖子。 我必须从OOP的角度解决这个问题,事情是我想用一个数组来保存用户输入的磁盘数量。我没有看到另一种方式。河内塔在C++中使用面向对象的概念
**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;
}
我开始认为使用这种方法无法解决问题。我在互联网上发现的这个问题的每个解决方案都是通过程序完成的。除了一些其他的语言和编程概念,我还没有被教过。这是我第二次参加过的编程课。感谢您的帮助。
谢谢!那么多... –