错误:构造函数期望并接收不同数量的参数,期望的主表达式错误,并且没有匹配的函数

时间:2019-02-11 20:30:01

标签: c++

我正在尝试从类中创建具有自定义类型的向量。它导致了我尝试解决的多个错误。

我尝试将括号更改为括号,并尝试使用名称空间运算符清除内容,但这些无效。我尝试为每个实例使用“ Vehicle :: Body_style :: SEDAN”,它似乎清除了我得到的所有错误。我只是得到了不同的东西,说我有一个未定义的参考。我不确定我是否正朝着正确的方向前进。我想我不是。

Main.cpp:


#include "Electric_vehicle.h"
#include <iostream>
#include <vector>

int main() {
    //enum class Body_style {SEDAN,HATCHBACK,SUV};
    std::vector<double> cost_per_kwh = {0.05, 0.08, 0.11, 0.13, 0.15};

    // For Bonus, refactor to read evs and ice from file 
    std::vector<Electric_vehicle> evs = {
        Electric_vehicle{2014, "Telsa", "Model S 85", Body_style::SEDAN, 3.12, 85},
        Electric_vehicle{2014, "Telsa", "Model 3 LR", Body_style::SEDAN, 4.13, 75},
        Electric_vehicle{2018, "GM", "Bolt", Body_style::HATCHBACK, 3.58, 60},
        Electric_vehicle{2018, "Nissan", "LEAF SL", Body_style::HATCHBACK, 3.88, 40},
    };
}

Electric_vehicle.h:

#ifndef __ELECTRIC_VEHICLE_H
#define __ELECTRIC_VEHICLE_H

#include <iostream>
#include "Vehicle.h"

class Electric_vehicle: public Vehicle {
    double _miles_per_kwh;
    double _max_kwh;
public:
    Electric_vehicle(int year, std::string make, std::string model, Vehicle::Body_style body_style, double miles_per_kwh, double max_kwh);
    double kwh_consumed(double miles);
};

#endif

在Electric_vehicle.cpp内部:

#include "Electric_vehicle.h"

Electric_vehicle::Electric_vehicle(int year, std::string make, std::string model, Vehicle::Body_style body_style, double miles_per_kwh, double max_kwh)
    : Vehicle(year, make, model, body_style), _miles_per_kwh{miles_per_kwh}, _max_kwh{max_kwh}{
}

double Electric_vehicle::kwh_consumed(double miles){
    double consume=miles/_miles_per_kwh;
    if(consume>_max_kwh){
        throw std::runtime_error("Gallons consumed is greater than max kwh.");
    }
    return consume;
}

Vehicle.h:

#ifndef __VEHICLE_H
#define __VEHICLE_H

#include <iostream>
#include <map>
#include <string>


class Vehicle{
public:
    enum class Body_style {SEDAN,HATCHBACK,SUV};
    Vehicle(int year, std::string make, std::string model, Body_style body_style);
    std::string make();
    std::string model();
    int year();
    Body_style body_style();
    std::string body_style_string();
    friend std::ostream &operator<<(std::ostream &out, Vehicle &V);
private:
    int _year;
    std::string _make;
    std::string _model;
    Body_style _body_style;
};

#endif

Vehicle.cpp

#include "Vehicle.h"

Vehicle::Vehicle(int year, std::string make, std::string model, Body_style body_style): _year{year}, _make{make}, _model{model}, _body_style{body_style}{

}

std::string Vehicle::make(){
    return _make;
}

std::string Vehicle::model(){
    return _model;
}
int Vehicle::year(){
    return _year;
}

Vehicle::Body_style Vehicle::body_style(){ //OPERATOR << for Vehicle
    return _body_style;
}

std::string Vehicle::body_style_string(){
    std::map<Body_style, std::string> tostring=
        {
            {Body_style::SEDAN, "Sedan"},
            {Body_style::HATCHBACK, "Hatchback"},
            {Body_style::SUV, "SUV"},
        };
        return tostring[_body_style];
}

std::ostream &operator<<(std::ostream &output, Vehicle &V){
    std::cout<<V._year<<" "<<V._make<<" "<<V._model<<" "<<V.Vehicle::body_style_string();
}

我希望它可以创建带有新对象的向量,因此以后可以调用它们。但是我却遇到了多个错误,例如:

main.cpp:12:25: error: expected primary-expression before ‘{’ token
main.cpp:12:55: error: ‘Body_style’ was not declared in this scope
main.cpp:12:55: error: no matching function for call to ‘Electric_vehicle::Electric_vehicle(<brace-enclosed initializer list>)’
main.cpp:12:25: error: could not convert ‘{<expression error>}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<Electric_vehicle>’

在我的Electric_vehicle.h文件中,我得到:

Electric_vehicle.h:11:2: note:   candidate expects 6 arguments, 4 provided
Electric_vehicle.h:7:7: note:   candidate expects 1 argument, 4 provided

0 个答案:

没有答案