4
#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;
}

我正在尝试为学校编写一个程序,该程序使用 Person* 和 Car* 类型的向量。这是我到目前为止所拥有的,代码编译得很好,但是当我开始填充人物向量时,程序崩溃了,我得到了一个异常错误。

我不想让任何人为我做作业,只要给我指出正确的方向,我就可以继续前进。

感谢你给与我的帮助。

显然代码不完整,我目前只是试图填充人员向量。一旦可行,我将继续讨论汽车矢量。

4

3 回答 3

5

您调用从未实例化的对象的成员函数,即从未分配的指针。也就是说,在调用之前,people[i]->read您应该为people[i]、likenew Person()或分配一些东西new Person(input_name,input_age)

一般来说,如果你有一行导致你的问题崩溃并且你不能通过阅读你的代码来解决它,你可能只是打印出所涉及的变量。喜欢people[i]。这可能NULL会给你一些想法。

于 2012-08-21T16:26:26.077 回答
1

在崩溃点,people[i]持有一个空指针,因此调用read()它会产生未定义的行为。至少,代码应该Person为每个指针创建一个对象。但:

难道people真的要持有指针?更自然的方法是让它简单地成为一个vector<Person>.

于 2012-08-21T16:31:03.903 回答
0
#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;
}

好的,谢谢大家的帮助,这里是“完成”的代码。它有效,但可能并不漂亮。任务是创建一个 Car 类,它有 3 个成员,汽车的型号、车主、具有姓名和年龄的 Person* 类型的对象以及汽车的司机,还有一个具有姓名和年龄的 Person*。从用户那里获取输入并将它们存储在 Car* 和 Person* 的向量中(为什么我不知道指针),将司机的年龄增加一年并输出汽车和司机。

再次感谢您让我朝着正确的方向前进。

希望获得有关如何使代码更高效并且仍然符合练习标准的反馈。

我把它按原样交了。

于 2012-08-24T04:10:28.433 回答