Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return the elements in an array using operator overloading with a class passing through it

I want to create a dynamic memory for a car dealership for bikes and cars but the vector didn’t print the right things and has lots of repetitions that aren’t supposed to be there.

I created an object for the overlapping variables for both vehicles so I can pass it through the bike class later . I tried to store all the elements in a vector and tried to overload the cout so it could print all elements inside the vector. I wanted to be able to have the vector use the object and print out all the elements correctly. Only the first 2 elements seem to be printing.

Main class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <iostream>
#include <algorithm>
#include "Car.h"
#include "Bike.h"
#include <list>
#include <cstdlib>
#include <vector>

using namespace std;

int main()
{
#ifdef _DEBUG
    _onexit(_CrtDumpMemoryLeaks);
#endif
    
    vector<Car> cars ;
    Car test = Car(2, "2", vehicleInfo("car", "M40 0J2", "lambo", "adventador", 3));
    Car test2 = Car(3, "4", vehicleInfo("car", "M40 0J2", "lambo", "adventador", 3));

    for (int i = 1; i <= 10; i++)
    {
        cars.push_back(test);
        cars.push_back(test2);
    }
    
    for (Car number : cars)
    {
        cout << number << endl;
    }
    return 0;
}

Car class

#include "Car.h"
#include <iostream>
#include <cstdlib>
Car::Car(int doors, string seats, const vehicleInfo& carinfo) : number_of_doors(doors), number_of_seats(seats), carInfo(carInfo)
{
}

Car::Car(const Car& src) : number_of_doors(src.number_of_doors), number_of_seats(src.number_of_seats), carInfo(src.carInfo)  //constant reference which initialises the variables
{   
}

Car::~Car()
{
}

std::ostream& operator<<(ostream& os, Car& rhs)
{
    return os<< rhs.number_of_doors << rhs.number_of_seats << rhs.carInfo; 
                   // attempted operator overload
}

Car.h

#include <iostream>
 
using namespace std;

class Car
{
public:
    Car(const Car& src);   // takes in constant reference 
    Car(int doors, string seats,const vehicleInfo& carinfo);
    ~Car();
    friend ostream& operator<<(ostream& os, Car& rhs);
private:
    int number_of_doors;
    string number_of_seats;
    string carInfo;
};

ostream& operator<<(ostream& os, Car& rhs);

VehicleInfo.h

#pragma once
#include <string>
using namespace std;

class vehicleInfo
{
public:
    //vehicleInfo() : type("N/A"), registration_number("N/A"), make("N/A"), age(0){}
    vehicleInfo(string type, string reg, string make, string model, int age);
    ~vehicleInfo();
private:
    string type;
    string registration_number;
    string make;
    string model;
    int age;
};

The output was

22
34
22
34
22
34
22
34
22
34
22
34
22
34
22
34
22
34
22
34

>Solution :

So there are multiple errors here. I’ll try and go through them one at a time.

The first error is in car.h

string carInfo;

Since you are trying to store vehicle info here, that should be vehicleInfo not string.

vehicleInfo carInfo;

Because car.h is now using vehicleInfo you should also add #include "VehicleInfo.h" to car.h

The second error is in car.cpp

Car::Car(int doors, string seats, const vehicleInfo& carinfo) : 
    number_of_doors(doors), number_of_seats(seats), carInfo(carInfo)
{
}

Notice that the vehicle info parameter is called carinfo, but when you try to use that parameter you instead typed carInfo. It should be like this

Car::Car(int doors, string seats, const vehicleInfo& carinfo) : 
    number_of_doors(doors), number_of_seats(seats), carInfo(carinfo)
{
}

The third error is also in car.cpp. You really should put spaces between the items you are printing. You should also declare the item you are printing as const

std::ostream& operator<<(ostream& os, const Car& rhs)
{
    return os << rhs.number_of_doors << ' ' << 
        rhs.number_of_seats << ' ' << 
        rhs.carInfo; 
}

The fourth error is perhaps the most important. If you want to print out vehicle info then you need to define an operator<< for vehicleInfo. You’ve already done this for Car, well it’s exactly the same for vehicleInfo. Something like this

std::ostream& operator<<(ostream& os, const vehicleInfo& rhs)
{
    return os << rhs.type<< ' ' << 
        rhs.reigstration_number << ' ' << 
        rhs.make<< ' ' << 
        rhs.model << ' ' << 
        rhs.age; 
}

Put this code in VehicleInfo.cpp and of course (just like Car) you need to add

friend std::ostream& operator<<(ostream& os, const vehicleInfo& rhs)

to VehicleInfo.h

That’s it (for now).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading