我是一名正在使用xcode開發旅遊指南程序的學生。我試圖設置Sport
類型的對象Ds
這是一個動態數組(我認爲是),其類型爲Attraction
。然後我想新Attraction
對象傳遞給setAttraction()
功能,但我得到的錯誤:上傳和傳遞動態數組(C++)
"cannot initialize a parameter of type 'Attraction**' to a lvalue of type 'Attraction*'.
現在,我是新來的OOP和C++,所以我不什麼意思。
main.cpp
:
int main() {
int x;
City Rome(111,"Rome");
City Dubai(222,"Dubai");
City Paris(333, "Paris");
cin >> x;
Sport* Ds = new Sport[x];
Culture* Dc = new Culture[x];
Shopping* Dh = new Shopping[x];
//{ new Sport, new Sport, new Sport, new Culture,
// new Culture, new Culture, new Shopping, new Shopping, new Shopping};
Ds[0].setName("Camel and Horse Racing");
Ds[1].setName("Marine Sports");
Ds[2].setName("Football");
Dc[0].setName("Dubai Museum");
Dc[1].setName("Falconry");
Dc[2].setName("Sheikh Saeed Al-Maktoum's House");
Dh[0].setName("Dubai Mall");
Dh[1].setName("Mall of Emirates");
Dh[2].setName("Deira City Centre");
Ds[0].setIDType(1);
Ds[1].setIDType(1);
Ds[2].setIDType(1);
Dc[0].setIDType(2);
Dc[1].setIDType(2);
Dc[2].setIDType(2);
Dh[0].setIDType(3);
Dh[1].setIDType(3);
Dh[2].setIDType(3);
//Here's the problem
Attraction* z = new Attraction[x];
*z = *Ds;
Dubai.setAttraction(z, x);
menu(Rome,Dubai,Paris);
return 0;
}
'City.cpp':
#include "City.h"
#include "Sport.h"
#include "Culture.h"
#include "Shopping.h"
#include <iostream>
#include "Attraction.h"
using namespace::std;
string City::getName(){
return name;
}
int City::getID(){
return id;
}
void City::setAttraction(Attraction a[], int size){
attractions = new Attraction[size];
for (int i = 0; i < size ; i++) {
attractions[i].setName(a[i].getName());//Here's the thread
cout << attractions[i].getName() << endl;
}
for (int j = 0; j < size ; j++) {
attractions[j].setIDType(a[j].getIDType());
}
}
void City::displayAttraction(){
cout << "There you go.\n\n";
for (int i = 0; i < 9 ; i++) {
cout << attractions[i].getName();
}
}
請閱讀如何創建一個最小化,完整和可驗證的示例:http://stackoverflow.com/help/mcve – Ziezi
「體育」,「文化」,「購物」是否繼承「吸引力」類? – user007
@simplicisveritatis對不起,但你能告訴我這個問題不是最小的,完整的或可驗證的,特別是在你編輯它之後,我感謝你。代碼太長,所以這是我可以做的最好的顯示問題。我還強調了代碼中問題的位置。 –