我只是寫一點OOP應用程序和運行時(不是編譯)上通過設置器類的私有變量的應用得到了崩潰,這裏的頭文件:代碼崩潰上設置類變量
class Car
{
private:
int year;
std::string brand;
std::string model;
int price;
std::string currency;
public:
int setYear(int x){this->year = x;}
std::string setBrand(std::string x){this->brand = x;}
std::string setModel(std::string x){this->model = x;}
int setPrice(int x){this->price = x;};
std::string setCurrency(std::string x){this->currency = x;}
};
和這裏的主: N - 對象的數量 溫度 - 傳遞整數 temp1中臨時變量 - 字符串傳遞
臨時變量ifstream fd("input.in");
int n;
fd >> n;
int temp;
string temp1;
Car A[n];
for(int i = 0; i < 3; i++)
{
fd >> temp;
A[i].setYear(temp);
fd >> temp1;
A[i].setBrand(temp1); //Crashes Here
fd >> temp1;
A[i].setModel(temp1);
fd >> temp;
A[i].setPrice(temp);
fd >> temp1;
A[i].setCurrency(temp1);
}
後的小測試,我想通了,它然後崩潰代碼嘗試設置「品牌」變量。有什麼問題?
'我<3'? 3從哪裏來? – JasonD
請回顧一下數組和向量之間的區別,你打破了編碼中的一個基本規則。我首先會建議使用矢量,但是如果你堅持使用數組,那麼你將需要在這個實例中動態地分配內存。 –