gpt4 book ai didi

c++ - 我的 C++ 代码给出了在代码中看不到的错误。错误是什么?

转载 作者:行者123 更新时间:2023-11-28 02:13:44 26 4
gpt4 key购买 nike

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

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');
}

最佳答案

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 传递给 string 构造函数,以便编译器将您的 null 参数传递给选项 2s 应该是:

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

string 构造函数试图取消引用 s 以将其内容复制到正在构造的 string 中时,它会出现段错误。

您在这里尝试构造的是一个空的字符串。当您使用默认构造函数时,C++ 已经这样做了:string()

如果在构造函数初始化列表中没有指定构造函数,将调用成员对象的默认构造函数。因此,您无需将 VehicleNocolor 放入构造函数初始化列表中即可将它们构造为空 string。这意味着您可以使用编译器生成的默认构造函数,并完全摆脱您的构造函数。

关于c++ - 我的 C++ 代码给出了在代码中看不到的错误。错误是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34745293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com