简体   繁体   中英

Trouble with pointers

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Person
{
    public:
    Person();
    Person(string input_name, int input_age);
    void read(string input_name, int input_age);
    void print() const;

    private:
    string name;
    int age;
};

Person::Person()
{
name = " ";
age = 0;
}

Person::Person(string input_name, int input_age)
{
name = input_name;
age = input_age;
}
void Person::read(string input_name, int input_age)
{
name = input_name;
age = input_age;
}

void Person::print() const
{
cout << "Name: " << name << " Age: " << age << endl;
}




class Car
{
public:
Car();
Car(string input_model, Person* p_input_owner, Person* p_input_driver);
void read(string input_model, Person* p_input_owner, Person* p_input_driver);
void print() const;

private:
string model;
Person* p_owner;
Person* p_driver;
};

Car::Car()
{
model = " ";
p_owner = NULL;
p_driver = NULL;
}

Car::Car(string input_model, Person* p_input_owner, Person* p_input_driver)
{
model = input_model;
p_owner = p_input_owner;
p_driver = p_input_driver;
}

void Car::read(string input_model, Person* p_input_owner, Person* p_input_driver)
{
model = input_model;
p_owner = p_input_owner;
p_driver = p_input_driver;
}

void Car::print() const
{
cout << "Model: " << model << " Owner: " << p_owner << " Driver: " << p_driver << endl;
}



int main()
{
vector<Person*> people(4);
vector<Car*> cars;
string input_name;
int input_age = 0;
string remainder;

for(int i = 0; i < people.size(); i++)          
{
    cout << "Enter the person's name: ";
    getline(cin, input_name);
    cout << "Enter the person's age: ";
    cin >> input_age;

    people[i]->read(input_name, input_age);
}

for(int i = 0; i < people.size(); i++)
{
    people[i]->print();
}

return 0;
}

I'm trying to write a program for school that uses a vector of type Person* and Car*. This is what I have so far, the code compiles fine but when I get to populating the people vector the program crashes and I get an exception error.

I don't want anyone to do the homework for me, just point me in the right direction so I can move on.

Thanks for any help you can give me.

Obviously the code is incomplete, I'm currently just trying to populate the people vector. Once that works I'll move on to the cars vector.

You call member function of the object that's never been instantiated, ie the pointer never assigned. That is, before calling people[i]->read you should assign something to people[i] , like new Person() or new Person(input_name,input_age) .

Generally, if you have line that crashes your problem and you can't figure it out by reading your code, you may just print out the variable involved. Like people[i] . It is probably NULL that would give you some idea.

At the point of the crash, people[i] holds a null pointer, so calling read() on it produces undefined behavior. At the very least, the code should create a Person object for each of the pointers. But:

Does people really have to hold pointers? The more natural approach would be for it to simply be a vector<Person> .

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>

using namespace std;
/*
Person class with members name and age
*/
class Person
{
public:
/*
default constructor
*/
Person();
/*
creates Person object with parameters
@param input_name the name of the person
@param input_age the age of the person
*/
Person(string input_name, int input_age); // constructor with inputs
/*
method to read person data
@param input_name the name of the person
@param input_age the age of the person
*/
void read(string input_name, int input_age);
/*
method that returns the age of the person
@return returns the age of the person
*/
int get_age() const;
/*
method that returns the name of the person
@return returns the name of the person
*/
string get_name() const;
/*
increments the age of the person by 1 year
*/
void update_age();

private:
string name;
int age;
};

Person::Person()
{
name = " ";
age = 0;
}

Person::Person(string input_name, int input_age)
{
name = input_name;
age = input_age;
}
void Person::read(string input_name, int input_age)
{
name = input_name;
age = input_age;
}

int Person::get_age() const
{
return age;
}

string Person::get_name() const
{
return name;
}

void Person::update_age()
{
age = age + 1;  
}

class Car
{
public:
/*
default constructor
*/
Car();
/*
creates an object of Car with parameters
@param input_model the model of the car
@param p_input_owner the owner of the car, type Person
@param p_input_driver the driver of the car, type Person
*/
Car(string input_model, Person* p_input_owner, Person* p_input_driver);
/*
method to read the car data
@param input_model the model of the car
@param p_input_owner the owner of the car, type Person
@param p_input_driver the driver of the car, type Person
*/
void read(string input_model, Person* p_input_owner, Person* p_input_driver);
/*
method that returns the model of the car
@return the model of the car
*/
string get_model() const;
private:
string model;
Person* p_owner;
Person* p_driver;
};

Car::Car()
{
model = " ";
p_owner = NULL;
p_driver = NULL;
}

Car::Car(string input_model, Person* p_input_owner, Person* p_input_driver)
{
model = input_model;
p_owner = p_input_owner;
p_driver = p_input_driver;
}

void Car::read(string input_model, Person* p_input_owner, Person* p_input_driver)
{
model = input_model;
p_owner = p_input_owner;
p_driver = p_input_driver;
}

string Car::get_model() const
{
return model;
}

int main()
{
int const NUMBER_OF_CARS = 3; // using a constant for the number of cars, can take this as input if desired
vector<Person*> people(NUMBER_OF_CARS * 2); // 2 people for each car, vector is 2 * NUMBER_OF_CARS
vector<Car*> cars(NUMBER_OF_CARS); // Sets the size of cars to the constant NUMBER_OF_CARS value

// declare variables
string input_name_owner;
string input_name_driver;
int input_age_owner = 0;
int input_age_driver = 0;
string remainder;
string input_model;
Person* temp_owner;
Person* temp_driver;


for(int i = 0; i < cars.size(); i++)            
{
    /*
    use 3 variables to increment the vectors to accurately to input the car, owner and driver
    Example of the math:
    a =      0   1   2   3   The cars vector elements
    a+b =    0   2   4   6   The people vector owner elements
    a+b+c =  1   3   5   7   The people vector driver elements
    no matter the size of the vectors, the input will populate the 
    vectors properly
    */
    int a = i;
    int b = i;
    int c = 1;

    // initialize all elements of the vectors
    cars[a] = new Car();
    people[a+b] = new Person();
    people[a+b+c] = new Person();
    // get user input
    cout << "Enter the model of the car: ";
    getline(cin, input_model);

    cout << "Enter the owner's name: ";
    getline(cin, input_name_owner);

    cout << "Enter the owner's age: ";
    cin >> input_age_owner;
    getline(cin, remainder); // empty the remainder of the input string

    cout << "\nEnter the driver's name: ";
    getline(cin, input_name_driver);

    cout << "Enter the driver's age: ";
    cin >> input_age_driver;
    getline(cin, remainder); // empty the remainder of the input string
    cout << endl;

    people[a+b]->read(input_name_owner, input_age_owner);
    people[a+b+c]->read(input_name_driver, input_age_driver);
    temp_owner = people[a+b];
    temp_driver = people[a+b+c];
    cars[a]->read(input_model, temp_owner, temp_driver);
}

for(int i = 0; i < cars.size(); i++)
{
    /*
    same theory for the output of the vector elements
    */
    int a = i;
    int b = i;
    int c = 1;
    cout << "Model: " << cars[a]->get_model() << endl;
    cout << "Owner's name: " << people[a+b]->get_name() << endl;
    cout << "Owner's age: " << people[a+b]->get_age() << endl;
    cout << "Driver's name: " << people[a+b+c]->get_name() << endl;
    cout << "Driver's age: " << people[a+b+c]->get_age() << endl;
    cout << endl;
}

// increment ages of owners and drivers by 1
for (int i = 0; i < people.size(); i++)
{
    people[i]->update_age();
}

// output the model, owner and driver with updated age
for(int i = 0; i < cars.size(); i++)
{
    int a = i;
    int b = i;
    int c = 1;
    cout << "Model: " << cars[a]->get_model() << endl;
    cout << "Owner's name: " << people[a+b]->get_name() << endl;
    cout << "Owner's age: " << people[a+b]->get_age() << endl;
    cout << "Driver's name: " << people[a+b+c]->get_name() << endl;
    cout << "Driver's age: " << people[a+b+c]->get_age() << endl;
    cout << endl;
}

// delete unused pointers   
for (int i = 0; i < cars.size(); i++)
{
    delete cars[i];
}
for (int i = 0; i < people.size(); i++)
{
    delete people[i];
}

// pause for user input
system("pause");

return 0;
}

Alright, thanks everyone for all the help, here is the "finished" code. It works, but probably isn't pretty. The assignment was to create a Car class that has 3 members, the model of the car, the owner, an object of type Person* with name and age and the driver of the car, also a Person* with name and age. Take inputs from the user and store them in vectors of Car* and Person* (why pointers I don't know), increment the ages of the drivers by one year and output the car and drivers.

Again, thanks for setting me in the right direction.

Would love feedback on how to make the code more efficient and still stay withing the criteria of the excercise.

I turned it in as is though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM