-4

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

#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

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    Output looks corecct to me, what were you expecting? – john Dec 15 '22 at 10:07
  • [Start learning how to debug your code please!](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?r=Saves_AllUserSaves) – πάντα ῥεῖ Dec 15 '22 at 10:08
  • You might understand what is happening a little better if you changed your `<<` to put spaces between the car items, like this `return os << rhs.number_of_doors << ' ' << rhs.number_of_seats << ' ' << rhs.carInfo;`. – john Dec 15 '22 at 10:10
  • you push the same cars into the vector several times in a loop. The output must contain repetitions, because thats whats inside the vector. – 463035818_is_not_an_ai Dec 15 '22 at 10:14
  • OK, I'm changing my mind, I'm not sure how this code compiles. In your `Car` constructor you have `carInfo(carInfo)` which does not seem to be legal, since `Car::carInfo` is a `string` and `carInfo` is type `vehicalInfo`. – john Dec 15 '22 at 10:15
  • I wanted it to print the vehicleInfo (the reg number, make model) but im not sure how. – James Pham Dec 15 '22 at 10:15
  • @JamesPham Can you answer my comment above about `carInfo`. That seems to be a problem with the code you've posted which doesn't make sense to me. – john Dec 15 '22 at 10:16
  • I was expecting each cout to be printing the values including make model and info, all the elements in the vector but it only prints out the first two values and not any of the vehicleinfo elements in the vector – James Pham Dec 15 '22 at 10:18
  • i think the vehicle info holds the data and it gets passed through car file and then i call it in main sorry if thats wrong im quite new to programming – James Pham Dec 15 '22 at 10:19
  • @JamesPham Yes there is something weird going on with car info, but your posted code does not make sense. For example here `Car::Car(int doors, string seats, const vehicleInfo& carinfo) : number_of_doors(doors), number_of_seats(seats), carInfo(carInfo)` See that you have `carInfo` in one place but `carinfo` in another. That doesn't work. – john Dec 15 '22 at 10:20
  • @James OK, I've figured it out. I'll try and write an answer. – john Dec 15 '22 at 10:22

1 Answers1

0

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).

john
  • 85,011
  • 4
  • 57
  • 81