C ++ class and objects ... Some extras needed

I wrote a class and object program and it is giving me some errors that I cannot get around. Here are some errors that I cannot get around, they are found in Main.cpp following errors ...

  Car car1("Ford", "Crown Victoria", 1997);
   Car car2("Chevrolet", "Camaro");
   Car car3("Hyundai", "Sonata", -15);
126 IntelliSense: no instance of constructor "Car::Car" matches the argument list
        argument types are: (const char [5], const char [15], int)

      

and

   car1.SetValue("Flinstones", "Rock Car", -2100);
   car3.SetValue("Toyota", "Camry", 2005);


132 IntelliSense: a reference of type "std::string &" (not const-qualified) cannot be initialized with a value of type "const char [6]"

      

Here is my code

// -----------------------Car.h-----------------------
#include <iostream>
#include <string>

class Car{
public:
    Car(string &make, string &model, int year=2015);        // constructor with three parameters
    string GetMake();
    string GetModel();
    int GetYear();
    int GetSpeed();

    bool SetValue(string &make, string &model, int year);   // set values from parameters
    bool Accelerate(char a);
    bool Brake(char b);
    void Display();                                                 // displays the output

private:
    string automake;
    string automodel;
    int autospeed;
    int autoyear;
};

// -----------------------------Car.cpp---------------------------
// The class definition for Car.
#include <iostream>
#include "Car.h"
#include <string>

using namespace std;

Car::Car(string &make, string &model, int year)
{
    automake = make;
    automodel = model;
    autospeed = 0;

    if(year < 0)
        year = 2015;
    else
        autoyear = year;
}

string Car::GetMake()
{
    return automake;
}

string Car::GetModel()
{
    return automodel;
}

int Car::GetYear()
{
    return autoyear;
}

int Car::GetSpeed()
{
    return autospeed;
}

bool Car::SetValue(string &make, string &model, int year)
{
    if(year < 0)
    {
        automake = make;
        automodel = model;
        autoyear = year;
        return true;
    }
    else
        return false;
}

void Car::Display()
{
    //cout <<"Your car is a " << autoyear << automodel << automake << endl;
    //cout <<"And it is currently going " << autospeed << " MPH." << endl;
}

bool Car::Accelerate(char a)
{
    if((a=='H')||(a=='h')||(a=='M')||(a=='m')||(a=='L')||(a=='l'))
    {
        if((a=='H')||(a=='h'))
            autospeed += 10;
        if((a=='M')||(a=='m'))
            autospeed += 5;
        if((a=='L')||(a=='l'))
            autospeed += 1;
        return true;
    }
    else
        return false;
}

bool Car::Brake(char b)
{
    if((b=='H')||(b=='h')||(b=='M')||(b=='m')||(b=='L')||(b=='l'))
    {
        if((b=='H'||b=='h' && autospeed > 10))
            autospeed = 10;
        if((b=='M'||b=='m' && autospeed > 5))
            autospeed = 5;
        if((b=='L'||b=='l' && autospeed > 1))
            autospeed = 1;
        return true;
    }
    else
        return false;
}

// -------------Main.cpp--------------------

// Driver routine to test the functions of the Car class

#include <iostream>
#include <string>
#include "Car.h"
#include "Car.cpp"

using namespace std;

int main()
{
   Car car1("Ford", "Crown Victoria", 1997);
   Car car2("Chevrolet", "Camaro");
   Car car3("Hyundai", "Sonata", -15);

   cout << "\n*** Displaying each car stats\n";
   cout << "Car1:\n";
   car1.Display();
   cout << "\nCar2:\n";
   car2.Display();
   cout << "\nCar3:\n";
   car3.Display();

   cout << "\n*** Accelerating car 3 several times:\n";

   car3.Accelerate('h');        // accelerate hard
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';

   car3.Accelerate('M');        // accelerate medium
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';

   car3.Accelerate('L');        // accelerate low
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';

   car3.Accelerate('L');        // accelerate low
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';

   car3.Accelerate('Z');        // accelerate with invalid level
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';

   cout << "\n*** Resetting car make/models\n";
   car1.SetValue("Flinstones", "Rock Car", -2100);
   car3.SetValue("Toyota", "Camry", 2005);

   cout << "Car1:\n";
   car1.Display();
   cout << "\nCar3:\n";
   car3.Display();

   cout << "\n*** Decelerating car3\n";
   car3.Brake('m');
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';
   car3.Brake('L');
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';
   car3.Brake('l');
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';
   car3.Brake('M');
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';
   car3.Brake('A');
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';
   car3.Brake('H');
   cout << "Car3 speed: " << car3.GetSpeed() << '\n';

   cout << "\n*** Calling accessors\n";
   cout << "Car1:\n";
   cout << "  Make:  " << car1.GetMake() << '\n'
        << "  Model: " << car1.GetModel() << '\n'
        << "  Year:  " << car1.GetYear() << '\n';

   cout << "Car2:\n";
   cout << "  Make:  " << car2.GetMake() << '\n'
        << "  Model: " << car2.GetModel() << '\n'
        << "  Year:  " << car2.GetYear() << '\n';

   cout << "Car1:\n";
   cout << "  Make:  " << car3.GetMake() << '\n'
        << "  Model: " << car3.GetModel() << '\n'
        << "  Year:  " << car3.GetYear() << '\n';

}

      

+3


source to share


2 answers


As the error says, you need to provide a constructor for the const

links:



Car(const string &make, const string &model, int year=2015);    

      

0


source


You can fix the problem by passing arguments by value rather than by reference:

Car(string make, string model, int year=2015);        // constructor 
bool SetValue(string make, string model, int year);  

      

This will automatically run the constructor std::string

that takes a as input const char *

.

The compiler will take care of optimizing the unused copies. You can add const

to your props, but it is generally redundant (and cumbersome) to add it when arguments are passed by value.




Also, in a C ++ element, initialization in a constructor should preferably be done like this ( list of initializers ):

Car::Car(string make, string model, int year)
  : automake(make), automodel(model), autospeed(0), autoyear(year < 0 ? 2015 : year)
{
//If the above autoyear code is not clear, you can perfectly
//keep your code for it, which was just fine
}

      

You must take care of initializing the members in the same order in which they are declared.

0


source







All Articles