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;
}
我開始認爲使用這種方法無法解決問題。我在互聯網上發現的這個問題的每個解決方案都是通過程序完成的。除了一些其他的語言和編程概念,我還沒有被教過。這是我第二次參加過的編程課。感謝您的幫助。
謝謝!那麼多... –