Reputation: 559
When I compile this code, it gives a runtime error shown below. But it doesn't tell which line is faulty in my code.
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.)
The following is my C++ code. Consisting one base class: Vehicle
and another derived class: Car
which is publicly inherited from the base class.
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');
}
Upvotes: 0
Views: 129
Reputation: 38919
C++ provides 9 string
constructors: http://en.cppreference.com/w/cpp/string/basic_string/basic_string
Of these 2 of them accept pointers:
basic_string(const CharT* s, size_type count, const Allocator& alloc = Allocator())
basic_string(const CharT* s, const Allocator& alloc = Allocator())
When you call VehicleNo(NULL)
and color(NULL)
you are only passing a null pointer and not a count
to the string
constructor so the compiler passes your null argument into option 2. Where s
is expected to be:
A pointer to a character string to use as source to initialize the string with
When the string
constructor attempts to dereference s
to copy it's contents into the string
being constructed it segfaults.
What you're trying to construct here is an empty string
. C++ already does that when you use the default constructor: string()
.
A member object's default constructor will be called if no construction is specified in the constructor intialization list. So you won't need to put either VehicleNo
or color
in the constructor intialization list to construct them as empty string
s. Meaning you can use the compiler's generated default constructor, and get rid of your constructor all together.
Upvotes: 4
Reputation: 548
Your problem is this line of code
Vehicle():VehicleNo(NULL),color(NULL){};
VehicleNO and color are of type string. They can't be NULL. Change it to something like this
Vehicle() :VehicleNo(" "), color(" "){};
Upvotes: -1