指针麻烦

时间:2012-08-21 16:24:21

标签: c++ class 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;
}

我正在尝试为学校编写一个使用Person *和Car *类型的矢量的程序。这是我到目前为止,代码编译得很好但是当我填充人员矢量时程序崩溃并且我得到异常错误。

我不希望任何人为我做作业,只是指出我正确的方向,所以我可以继续前进。

感谢您提供任何帮助。

显然代码不完整,我目前只是试图填充人物矢量。一旦有效,我将转向汽车矢量。

3 个答案:

答案 0 :(得分:5)

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

通常情况下,如果你有一行崩溃你的问题并且你无法通过阅读你的代码弄清楚它,你可能只打印出所涉及的变量。像people[i]一样。可能会NULL给你一些想法。

答案 1 :(得分:1)

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

people真的必须保持指针吗?更自然的方法是简单地成为vector<Person>

答案 2 :(得分: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 *类型的对象,名称和年龄以及汽车的驾驶员,还有一个名字和年龄的人*。从用户那里获取输入并将它们存储在Car *和Person *的向量中(为什么指针我不知道),将驱动程序的年龄增加一年并输出汽车和驱动程序。

再一次,谢谢你让我朝着正确的方向前进。

如果能够提高代码的效率并且仍然遵循练习的标准,我们会喜欢反馈。

我按原样把它打开了。