对象问题C ++

时间:2014-04-09 18:24:49

标签: c++ visual-studio oop

我没有错误,它构建并运行但它没有显示车辆类型。所有帮助赞赏。我试图存储字符串" car"在我的对象但它不工作。

string vType[] = {"Car","Van","Lorry","Motorbike","Tanker"};

我尝试使用:

 Vehicle one;
one.setVehicleType(vType[0]);
cout << "Vehicle Type:" << one.getVehicleType() << endl;

Vehicle.ccp

#include "Vehicle.h"
Vehicle::Vehicle() {
vehicleMake = ' ';
}

Vehicle::Vehicle(string type){  
    vehicleVehicleType = type;

}

Vehicle::~Vehicle(){

}





//getMethods


string Vehicle::getFuelType()const{
    return vehicleFuelType;
}

string Vehicle::getVehicleType()const{
    return vehicleVehicleType;
}




//setMethods

void Vehicle::setMake(string make){
    vehicleMake = make;
}


void Vehicle::setVehicleType(string type){
    vehicleFuelType = type;
}

Vehicle.h

    class Vehicle{

    public:     
    //Default Constructor
        Vehicle();

    //Overload constructor
        Vehicle(char, string, string, string, string, int, int, int, int);

    //Destructor
        ~Vehicle();

    //Accessor Functions
        //get methods
        string vehicleType() const;

    void setVehicleType(string);
        //setFuelType - for Vehicle

    protected:  
        string vehicleVehicleType;

2 个答案:

答案 0 :(得分:2)

看起来你在这里设置了错误的成员:

void Vehicle::setVehicleType(string type){
    vehicleFuelType = type;
}

你可能意味着

void Vehicle::setVehicleType(string type){
    vehicleVehicleType = type;
}

答案 1 :(得分:0)

我认为你在setter函数中设置了错误的字段:

void Vehicle::setVehicleType(string type){
    vehicleFuelType = type;
}

应该是

void Vehicle::setVehicleType(string type){
    vehicleVehicleType = type;
}

并且,由于您拥有适当的构造函数,因此这是创建对象的更好方法:

Vehicle one(vType[0]);