0

I was given the main and told to create a program in response to the main using 3 classes. An amusement park class, a rider class, and a ride class. I want to add a rider to a vector of rides and store that vector in a vector of amusement parks. What am I doing wrong? How can I fix this?

#include <iostream>
#include <string>

using namespace std;

class Rider
{
string name;
int height;
public:
    Rider(string name, int height)
    {
        this->name=name;
        this->height=height;
    }
    Rider(int height)
    {
        this->height=height;
    }
};
class Ride
{
    public:
    vector <Rider> all_riders;
};
class Amusement_park
{
    vector <Ride> all_rides;
    public:
        Amusement_park(int numRides)
        {
            all_rides[numRides];
        }
        vector <Rider> get_ride(int whichRide)
        {
            vector <Ride> the_ride= all_rides[whichRide];
            return the_ride;
        }
        void add_line(class Rider)
        {
            the_ride.pushback(Rider);
        }
};
int main()
{
    Rider r1("Yaris",45);   //name, height in inches
    Rider r2(49);           //height in inches
    Amusement_park a1(3);  //3 is the number of rides in the park
    a1.get_ride(1).add_line(r1); //add a rider to the line of a ride
    Amusement_park a2(2); //2 is the number of rides in the park
    a2.get_ride(1).add_line(r2); //add a rider to the line of a ride
    return  0;
}

2 Answers2

0

In your function add_line(class Rider), there is no reference of the_ride to push the rider. You can pass the vector<Rider> you get from get_ride(int whichRide) to the function or you can just combine the get_ride and add_line to one function like this:

void add_rider_to_line(int whichRide, class Rider)
{
    all_rides[whichRide].push_back(Rider);
}
Sudip Sarker
  • 421
  • 3
  • 7
0

I'll give some advices for your code, and one (or more) of them will presumably solve problems you might have.

First, always try to use constructor initializer lists when possible. Usually, when the constructor is obvious as is yours, using such a list is trivial. In this case, you could just do:

    class Rider
    {
    string name;
    int height;
    public:
        Rider(string name, int height) : name(name), height(height) {}
        Rider(int height) : height(height) {}
    };

and the same for the rest of your trivial constructors.

Now, if you take a closer look at your c'tor for Amusement_park class, you will see that you try to access an index in your vector that hasn't been, let's say, defined yet. That is, you have an empty vector that is supposed to hold objects of Ride type, but since it is empty, trying to access its content (whatever index you might try to reach) is what we call undefined behavior. That's a piece of memory that you don't know what it holds, and that's because you never told your compiler how many Ride objects will be in there so that your program could properly allocate memory and initialize objects for you there. In order for you to actually create a vector of the appropriate size, or, more precisely, properly resize your vector, your Amusement_park c'tor should be something like

Amusement_park(int numRides)
    {
        all_rides.resize(numRides);
    }

Your get_ride function inside Amusement_park is also not very good. Actually, your whole statement a1.get_ride(1).add_line(r1) isn't doing what you are intending to. First, the get_ride() function returns a vector of Rider, which doesn't have an add_line() member function. You actually want it to return a Amusement_park object, which is the only one in your code that has such a member function. However, returning it is not logical, as you might see.

In short, your code contains many flaws, both language as logically speaking. Hence, I recommend that you: 1) Double check the logic of your code; 2) Get a nice tutorial book on C++ to start on.

Good luck!

Eron
  • 116
  • 4
  • I've fixed some things according to what you said, and now I only have one error message. I moved the add_line function into my Ride class. I'm now getting an error that says [error: 'Rider' does not refer to a value all_riders.push_back(Rider); ] –  Sep 25 '19 at 17:19
  • Did you modify your `get_ride()` function inside `Amusement_park`? it should return a `Ride` object, not a `vector` as it is in your code. For this, all you need to do is to modify the return type and then return the value `all_rides[whichRide]`. Then you could call the function `add_line()` inside the `Ride` class through the object returned by your `get_ride()` function. Also, what line your compiler is pointing to when raising that error message? – Eron Sep 25 '19 at 17:34
  • Also, the parameter of your `add_line()` function is not correct. `Rider` is a type by itself, defined by you, actually. So, your function, logically, should expect a variable with type `Rider`, and hence its declaration should be `void add_line(Rider riderId)`, where riderId is the variable name, and Rider is its type. Then, you push riderId to your vector as usual. – Eron Sep 25 '19 at 17:46
  • I did what you said and the error was pointing to the add_line function inside my Ride class. That second comment just solved it. I was using (class Rider) as my parameter instead of (Rider (variable name)). Thank you! –  Sep 25 '19 at 17:50
  • I think that this will fix your problem at hand. However, please try to follow my 2nd suggestion/recommendation. There are plenty of good options here [https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list] – Eron Sep 25 '19 at 17:50