我写了一个类和对象程序,它给了我一些我无法避免的错误。这里有一些我无法解决的错误,它们在 Main.cpp 中找到以下错误....
Car car1("Ford", "Crown Victoria", 1997);
Car car2("Chevrolet", "Camaro");
Car car3("Hyundai", "Sonata", -15);
126 IntelliSense: no instance of constructor "Car::Car" matches the argument list
argument types are: (const char [5], const char [15], int)
和
car1.SetValue("Flinstones", "Rock Car", -2100);
car3.SetValue("Toyota", "Camry", 2005);
132 IntelliSense: a reference of type "std::string &" (not const-qualified) cannot be initialized with a value of type "const char [6]"
这是我的代码
// -----------------------Car.h-----------------------
#include <iostream>
#include <string>
class Car{
public:
Car(string &make, string &model, int year=2015); // constructor with three parameters
string GetMake();
string GetModel();
int GetYear();
int GetSpeed();
bool SetValue(string &make, string &model, int year); // set values from parameters
bool Accelerate(char a);
bool Brake(char b);
void Display(); // displays the output
private:
string automake;
string automodel;
int autospeed;
int autoyear;
};
// -----------------------------Car.cpp---------------------------
// The class definition for Car.
#include <iostream>
#include "Car.h"
#include <string>
using namespace std;
Car::Car(string &make, string &model, int year)
{
automake = make;
automodel = model;
autospeed = 0;
if(year < 0)
year = 2015;
else
autoyear = year;
}
string Car::GetMake()
{
return automake;
}
string Car::GetModel()
{
return automodel;
}
int Car::GetYear()
{
return autoyear;
}
int Car::GetSpeed()
{
return autospeed;
}
bool Car::SetValue(string &make, string &model, int year)
{
if(year < 0)
{
automake = make;
automodel = model;
autoyear = year;
return true;
}
else
return false;
}
void Car::Display()
{
//cout <<"Your car is a " << autoyear << automodel << automake << endl;
//cout <<"And it is currently going " << autospeed << " MPH." << endl;
}
bool Car::Accelerate(char a)
{
if((a=='H')||(a=='h')||(a=='M')||(a=='m')||(a=='L')||(a=='l'))
{
if((a=='H')||(a=='h'))
autospeed += 10;
if((a=='M')||(a=='m'))
autospeed += 5;
if((a=='L')||(a=='l'))
autospeed += 1;
return true;
}
else
return false;
}
bool Car::Brake(char b)
{
if((b=='H')||(b=='h')||(b=='M')||(b=='m')||(b=='L')||(b=='l'))
{
if((b=='H'||b=='h' && autospeed > 10))
autospeed = 10;
if((b=='M'||b=='m' && autospeed > 5))
autospeed = 5;
if((b=='L'||b=='l' && autospeed > 1))
autospeed = 1;
return true;
}
else
return false;
}
// -------------Main.cpp--------------------
// Driver routine to test the functions of the Car class
#include <iostream>
#include <string>
#include "Car.h"
#include "Car.cpp"
using namespace std;
int main()
{
Car car1("Ford", "Crown Victoria", 1997);
Car car2("Chevrolet", "Camaro");
Car car3("Hyundai", "Sonata", -15);
cout << "\n*** Displaying each car's stats\n";
cout << "Car1:\n";
car1.Display();
cout << "\nCar2:\n";
car2.Display();
cout << "\nCar3:\n";
car3.Display();
cout << "\n*** Accelerating car 3 several times:\n";
car3.Accelerate('h'); // accelerate hard
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Accelerate('M'); // accelerate medium
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Accelerate('L'); // accelerate low
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Accelerate('L'); // accelerate low
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Accelerate('Z'); // accelerate with invalid level
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
cout << "\n*** Resetting car make/models\n";
car1.SetValue("Flinstones", "Rock Car", -2100);
car3.SetValue("Toyota", "Camry", 2005);
cout << "Car1:\n";
car1.Display();
cout << "\nCar3:\n";
car3.Display();
cout << "\n*** Decelerating car3\n";
car3.Brake('m');
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Brake('L');
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Brake('l');
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Brake('M');
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Brake('A');
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
car3.Brake('H');
cout << "Car3 speed: " << car3.GetSpeed() << '\n';
cout << "\n*** Calling accessors\n";
cout << "Car1:\n";
cout << " Make: " << car1.GetMake() << '\n'
<< " Model: " << car1.GetModel() << '\n'
<< " Year: " << car1.GetYear() << '\n';
cout << "Car2:\n";
cout << " Make: " << car2.GetMake() << '\n'
<< " Model: " << car2.GetModel() << '\n'
<< " Year: " << car2.GetYear() << '\n';
cout << "Car1:\n";
cout << " Make: " << car3.GetMake() << '\n'
<< " Model: " << car3.GetModel() << '\n'
<< " Year: " << car3.GetYear() << '\n';
}
最佳答案
如错误所述,您需要为 const
引用提供构造函数:
Car(const string &make, const string &model, int year=2015);
关于c++ - C++ 中的类和对象......需要一些额外的固定装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29906446/