Linked Questions

Popular Questions

I am trying to create a vector with a custom type from a class. It is causing multiple errors that I've tried solving.

I've tried changing brackets to parenthesis, and tried using the namespace operator to clear things up, but those haven't worked. I have tried using "Vehicle::Body_style::SEDAN" for each instance and it seems to clear all of the errors I've gotten. I just get different ones, saying I have an undefined reference. I'm not sure if I was heading in the right direction or not with that fix. I don't think I was.

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

Inside 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();
}

I expect it to create a vector with new objects, so I can call upon them later. But I instead get multiple errors such as:

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>’

Inside my Electric_vehicle.h file, I am getting:

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

Related Questions