类和对象在C++..需要一些额外的固定装置
Class and Objects in C++ ...Need Some Extra Fixtures
我写了一个类和对象程序,它给了我一些我无法解决的错误。这里有一些我无法解决的错误,它们在 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 statsn";
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/modelsn";
car1.SetValue("Flinstones", "Rock Car", -2100);
car3.SetValue("Toyota", "Camry", 2005);
cout << "Car1:n";
car1.Display();
cout << "nCar3:n";
car3.Display();
cout << "n*** Decelerating car3n";
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 accessorsn";
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);
您可以通过按值传递参数而不是按引用传递参数来解决问题:
Car(string make, string model, int year=2015); // constructor
bool SetValue(string make, string model, int year);
这将允许自动触发std::string
的构造函数,该构造函数将const char *
作为输入。
编译器将负责优化未使用的副本。您可以向输入参数添加const
,但是当参数按值传递时,添加它通常是多余的(而且很麻烦)。
此外,在构造函数中C++成员初始化最好通过以下方式进行(初始值设定项列表):
Car::Car(string make, string model, int year)
: automake(make), automodel(model), autospeed(0), autoyear(year < 0 ? 2015 : year)
{
//If the above autoyear code is not clear, you can perfectly
//keep your code for it, which was just fine
}
您必须注意按照声明成员的相同顺序初始化成员。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 类和对象在C++..需要一些额外的固定装置