C++ 我的C++;代码给出了代码中未看到的错误。有什么问题?

C++ 我的C++;代码给出了代码中未看到的错误。有什么问题?,c++,oop,syntax,C++,Oop,Syntax,当我编译这段代码时,它给出了如下所示的运行时错误。但它不能告诉我的代码中哪一行有错误 调试断言失败 程序:C:\Windows\system32\MSVCP110D.dll 文件:c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xstring 行:1143 表达式:无效的空指针 有关您的程序如何导致断言失败的信息,请参见Asvest.VisualC++文档。 (按“重试”调试应用程序。) 以下是我的C++代码。由一个基类:

当我编译这段代码时,它给出了如下所示的运行时错误。但它不能告诉我的代码中哪一行有错误

调试断言失败

程序:C:\Windows\system32\MSVCP110D.dll
文件:c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xstring
行:1143

表达式:无效的空指针

有关您的程序如何导致断言失败的信息,请参见Asvest.VisualC++文档。 (按“重试”调试应用程序。)

以下是我的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');
}
class车辆{
私人:
字符串,颜色;
公众:
Vehicle():VehicleNo(NULL),color(NULL){};
字符串getVehicleNo(){return VehicleNo;}
字符串getColor(){return color;}
void setVehicleNo(){
//getline(cin、VehicleNo);
cin>>VehicleNo;
}
void setVehicleColor(){cin>>color;}
};
车辆类别:公共车辆{
私人:
整数距离;
公众:
void setDistance(int x){distance=x;}
void setCarNo(){setVehicleNo();}
void setCarColor(){setVehicleColor();}
int calculateFare(int x){返回5*x;};
void displayInformation()
{

你的问题是这行代码

Vehicle():VehicleNo(NULL),color(NULL){};
VehicleNO和color是字符串类型。它们不能为NULL。请将其更改为类似以下内容

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

您的问题是这行代码

Vehicle():VehicleNo(NULL),color(NULL){};
VehicleNO和color是字符串类型。它们不能为NULL。请将其更改为类似以下内容

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

C++提供了9个
string
构造函数:

其中2个接受指针:

  • basic\u字符串(常量图表*s、大小\u类型计数、常量分配器&alloc=Allocator())
  • basic_字符串(常量图表*s,常量分配器&alloc=Allocator())
  • 调用
    VehicleNo(NULL)
    color(NULL)
    时,只向
    string
    构造函数传递一个NULL指针,而不是
    count
    ,因此编译器将NULL参数传递到选项2。其中
    s
    应为:

    指向用作初始化字符串源的字符串的指针

    string
    构造函数尝试取消引用
    s
    以将其内容复制到正在构造的
    字符串中时,它会出错

    这里你想构造的是空的<>代码>字符串/c>。C++在使用默认构造函数时已经做到了:<代码> String()< < /> >


    如果构造函数初始化列表中未指定任何构造,则将调用成员对象的默认构造函数。因此,无需在构造函数初始化列表中放入
    VehicleNo
    color
    ,即可将其构造为空
    字符串
    s。这意味着您可以使用编译器生成的默认构造函数构造函数,并一起去掉构造函数。

    C++提供了9个
    string
    构造函数:

    其中2个接受指针:

  • basic\u字符串(常量图表*s、大小\u类型计数、常量分配器&alloc=Allocator())
  • basic_字符串(常量图表*s,常量分配器&alloc=Allocator())
  • 调用
    VehicleNo(NULL)
    color(NULL)
    时,只向
    string
    构造函数传递一个NULL指针,而不是
    count
    ,因此编译器将NULL参数传递到选项2。其中
    s
    应为:

    指向用作初始化字符串源的字符串的指针

    string
    构造函数尝试取消引用
    s
    以将其内容复制到正在构造的
    字符串中时,它会出错

    这里你想构造的是空的<>代码>字符串/c>。C++在使用默认构造函数时已经做到了:<代码> String()< < /> >


    如果构造函数初始化列表中未指定任何构造,则将调用成员对象的默认构造函数。因此,无需在构造函数初始化列表中放入
    VehicleNo
    color
    ,即可将其构造为空
    字符串
    s。这意味着您可以使用编译器生成的默认构造函数如果是不同的语言,请在询问时小心标记。除非问题的性质需要,否则不要发布图像。关于错误:使用调试器。为什么要用
    NULL
    s初始化
    std::string
    类型的变量?编译以进行调试,在de中运行bugger,当它打开显示当前调用堆栈的窗格时,请单击代码中的行。我确信您确实“按重试以调试应用程序”,不是吗…?而且是不同的语言,在询问时请小心标记。除非问题的性质需要,否则不要发布图像。关于错误:使用调试器。为什么要用
    NULL
    s初始化
    std::string
    类型的变量?编译以进行调试,在调试器中运行,当它打开显示当前调用堆栈,单击代码中的行。我确信您确实“按重试以调试应用程序”,不是吗…?我只是想去掉它,因为编译器提供的构造函数就是所需的全部。为什么要将它们初始化为一个空格字符串?!为什么要用字符数组初始化它们?为什么要手动初始化它们,而编译器会正确地为您做所有事情?我只想去掉它,因为编译器提供的构造函数是所有这些都是必需的。为什么要将它们初始化为一个空格的字符串?!为什么要用字符数组初始化它们?为什么要手动初始化它们,而编译器会正确地为您执行所有操作?“it segfults”-更多pr