2017-02-15 116 views
0

我需要创建一个程序,它有3个集合,3个以及构造函数。然而,当我创建的默认构造函数,它给了我,指出我们需要有一个“)”前“”排队默认构造函数C++格式

#include <string> 

class Vehicle 
{ 
public: 
    Vehicle(std::string vehicleType, int numberOfDoors, int maxSpeed) 
     : type{vehicleType}, number{numberOfDoors}, speed{maxSpeed}{} 
    void setType(std::string vehicleType) { 
       type = vehicleType;} 
    void setNumber(int numberOfDoors){ 
        number = numberOfDoors;} 
    void setSpeed(int maxSpeed) { 
       speed = maxSpeed;} 

    Vehicle(string, int, int); 
    ~Vehicle(); 
    Vehicle(); 
    std::string getType() const {return type;} 
    int getNumber() const {return number;} 
    int getSpeed() const {return speed;} 

private: 
    std::string type; 
    int number; 
    int speed; 
}; 

有人能告诉我什么是错的错误?

+0

'汽车(字符串,INT,INT);' - 它应该是'的std :: string'。 'string'不是这里的类型的名字。 – yeputons

+0

为什么你声明两次相同的构造函数? '车辆(std :: string vehicleType,int numberOfDoors,int maxSpeed)'和'Vehicle(string,int,int)'。 – iosdude

+0

@iosdude 如果我把它放在一边,它会告诉我未识别的车辆参考:车辆,这意味着没有默认构造函数 – xx123

回答

0

您需要删除Vehicle(string, int, int);,因为它已经被定义。 (检查你的第一个构造函数)

+0

如果我删除Vehicle(string,int,int); 然后它告诉我我需要一个默认的构造函数。它给了我一个“Vehicle :: Vehicle”的无法识别的引用 – xx123

+0

您可以删除'Vehicle();'和'〜Vehicle();' 或者像@swapnil建议的那样 - 'Vehicle()= default;'和'〜 Vehicle()= default;'如果在C++ 11上。 – grubs

1

你已经在你的类的开头定义了三个参数构造函数,你使用成员初始值设定项列表初始化类Vehicle的成员变量。因此,您不需要再次声明它:

Vehicle(string, int, int); 

如果您删除此行,您的代码将被编译。见here

当你实例化你的类,你需要提供三个参数,一个string和两个int小号

此外,如果你想有一个默认的构造,可以将下面一行添加到您的类。

Vehicle():type(), number(0), speed(0){} 

那么你应该能够实例化Vehicle类不带任何参数和用户setter函数值设置为这样一个对象的成员变量。

而且要么删除没有定义析构函数或定义的析构函数,也许是这样的:

~Vehicle(){ type.clear();} 

here