实例化一个新对象 - 错误

时间:2013-11-17 20:00:10

标签: c++ inheritance constructor

#include <iostream>
using namespace std;

class Vehicle{
protected:
    string type;
    int wheels;
    bool engine; // number of engines in vehicle

public:
   Vehicle(string t, int w,bool e):
        type(t), wheels(w), engine(e){};
    void setType(string t) {type = t;}
    void setWheels(int w) {wheels = w;}
    void setEngine(int e) {engine = e;} // number of engines, 0 - False.
    string getType(){return type;}
    int getWheels() {return wheels;}
    bool getEngine() {cout << "1 - Has Engine | 0 - No Engine"; return engine;}


};

class Auto:public Vehicle {
private:
    string brand;
    int year;
public:
    Auto(string t, int w, bool e, string b, int y):
        Vehicle(t,w,e), brand(b),year(y) {};

    void setBrand(string b) {brand = b;}
    void setYear(int y) {year = y;}
    string getBrand() {return brand;}
    int getYear() {return year;}
};

int main()
{
    // This first segment of the program demonstrates the relationship
   // between the base class and derived class through the use of
  // a constructor.
Auto Spider360("Car",4,2,"Ferrari",2000);
cout << "Car type: " << Spider360.getType() << endl;
cout << "Number of wheels: " << Spider360.getWheels() << endl;
cout << " Has Engine: " << Spider360.getEngine() << "\n";
cout << "Brand: " << Spider360.getBrand() << endl;
cout << "Year: " << Spider360.getYear() << "\n\n";

// Now I use member functions directly to assign values to an object

Auto SuperAmerica;

return 0;

}

我无法声明对象Auto SuperAmerica;我收到以下错误:“没有匹配的函数调用Auto :: Auto()”和SuperAmerica,我不想使用构造函数来设置值,我想使用我的Set函数。

1 个答案:

答案 0 :(得分:0)

的错误
"No matching function call for Auto::Auto()"

表示您无法以您希望的方式实例化该类。如果要创建对象,然后使用setter初始化其成员,请使用default constructor