2016-01-12 55 views
0

當我編譯這段代碼時,它給出瞭如下所示的運行時錯誤。但它並沒有告訴我代碼中哪條線路有問題。我的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'); 
} 
+1

[tag:c]和[tag:C++]是不同的語言,請問時要小心標籤。 –

+0

除非問題的性質需要它們,否則不要發佈圖像。關於錯誤:使用調試器。 – Olaf

+2

你爲什麼用'NULL'初始化'std :: string'類型的變量? – ForceBru

回答

-1

你的問題是這行代碼

Vehicle():VehicleNo(NULL),color(NULL){}; 

VehicleNO和顏色都是字符串類型的。它們不能爲NULL。將其更改爲這樣的事情

Vehicle() :VehicleNo(" "), color(" "){}; 
+5

我只是擺脫它,因爲編譯器提供的構造函數是所有需要的。 – NathanOliver

+0

@NathanOliver [我同意](http://stackoverflow.com/a/34745705/2642059) –

+0

爲什麼將它們初始化爲單空間字符串?爲什麼要用char數組初始化它們?爲什麼初始化它們*手動*當編譯器會正確地爲你做所有事情時? –

3

C++提供了9個string構造函數:http://en.cppreference.com/w/cpp/string/basic_string/basic_string

這2人都接受指針:

  1. basic_string(const CharT* s, size_type count, const Allocator& alloc = Allocator())
  2. basic_string(const CharT* s, const Allocator& alloc = Allocator())

當你打電話給VehicleNo(NULL)color(NULL)您只傳遞一個空指針,而不是構造函數的count,因此編譯器會將您的空參數傳遞到選項。凡s預計:

A pointer to a character string to use as source to initialize the string with

string構造函數試圖取消引用s複製它的內容到string正在建造它出現segfaults。

你要在這裏構建的是一個空的string。當你使用默認構造函數時,C++已經這樣做了:string()

如果在構造函數初始化列表中未指定構造,則將調用成員對象的默認構造函數。因此,您不需要將VehicleNocolor放置在構造函數初始化列表中,將它們構造爲空的string s。這意味着你可以使用編譯器生成的默認構造函數,並且一起擺脫構造函數。

+0

「它segfaults」 - 更確切地說,行爲是不確定的。 –

+0

@ChristianHackl是不是從'NULL'讀取總是出現段錯誤?您的評論似乎暗示情況並非如此。有可能會出現'NULL'被讀取的情況,但是隻會產生一個未定義的行爲,而不是segfault。 –

+0

我的意思是說,將空指針傳遞給這兩個構造函數已經是未定義的行爲。然後構造函數將從參數中讀取(從而導致崩潰)的事實只是一個實現細節。再次,從空指針讀取也總是未定義的行爲。 –