當我編譯這段代碼時,它給出瞭如下所示的運行時錯誤。但它並沒有告訴我代碼中哪條線路有問題。我的C++代碼給出了在代碼中看不到的錯誤。錯誤是什麼?
Debug Assertion Failed!
Program: C:\Windows\system32\MSVCP110D.dll
File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring
Line: 1143
Expression: invalid null pointer
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application.)
以下是我的C++代碼。包含一個基類:Vehicle
和另一個派生類:Car
,它從基類公開繼承。
class Vehicle {
private:
string VehicleNo, color;
public:
Vehicle():VehicleNo(NULL),color(NULL){};
string getVehicleNo(){return VehicleNo;}
string getColor(){return color;}
void setVehicleNo(){
//getline(cin,VehicleNo);
cin>>VehicleNo;
}
void setVehicleColor(){cin>>color;}
};
class Car: public Vehicle {
private:
int distance;
public:
void setDistance(int x){distance=x;}
void setCarNo(){setVehicleNo();}
void setCarColor(){setVehicleColor();}
int calculateFare(int x){return 5*x;};
void displayInformation()
{
cout<<"Your Car Number is: "<<getVehicleNo()<<endl;
cout<<"The color of your Car is: "<<getColor()<<endl;
cout<<"Total fare which you have to pay: "<<calculateFare(distance);
}
};
int main()
{
Car c1;
int distance;
char choice;
cout<<"Enter car number: ";
c1.setCarNo();
cout<<"\nEnter Car Color: ";
c1.setCarColor();
cout<<"\nHow long would you like to go? Enter distance in kilometers: ";
cin>>distance;
c1.setDistance(distance);
cout<<"\n----------------------------------\n";
c1.displayInformation();
cout<<"\n----------------------------------\n";
cout<<"\nDo you want to calculate Fare of different distance (y/Y for yes and another character for No? ";
cin>>choice;
do{
cout<<"\nHow long would you like to go? Enter distance in Kilometers: ";
cin>>distance;
cout<<"\n----------------------------------\n";
c1.setDistance(distance);
c1.displayInformation();
cout<<"\nDo you want to calculate Fare of different distance (y/Y for yes and another character for No? ";
cin>>choice;
}
while(choice=='y' || choice=='Y');
}
[tag:c]和[tag:C++]是不同的語言,請問時要小心標籤。 –
除非問題的性質需要它們,否則不要發佈圖像。關於錯誤:使用調試器。 – Olaf
你爲什麼用'NULL'初始化'std :: string'類型的變量? – ForceBru