【问题标题】:Changing value of element in dynamically allocated array更改动态分配数组中元素的值
【发布时间】:2018-03-06 18:07:06
【问题描述】:

我有两个动态分配的类对象数组 - 学生和教职员工。当用户输入年龄时,我想根据年龄更新学生数组或人员数组的元素。 但是我下面的代码不起作用。一旦分配给学生,变量 person 就不会被重新分配给工作人员。无论我输入的年龄如何,我输入的所有数据都只会进入学生。我的代码有什么问题?如何拥有一个变量并根据条件检查为其分配一个或其他数组元素?

#include <iostream>
using namespace std;

int main()
{
    class info
    {
    public:
        int unique_id;
        char* hair_color;
        int height;
        int weight;
    };

    class info* student;
    student = new info[10];

    class info* staff;
    staff = new info[10];

    for (int i=0; i<10;i++)
    {
        class info& person = student[i];

        int age ;
        cout<< "enter age"<<endl;
        cin >> age;

        if( age > 18 )
        {
            person = staff[i];  // This assignment doesn't work ??
        }
        cout<< "enter unique_id"<<endl;
        cin >> person.unique_id;
        cout<< "enter height"<<endl;
        cin >> person.height;
        cout<< "enter weight"<<endl;
        cin >> person.weight;

    }

    cout<<" Student "<<student[0].unique_id<<"   "<<student[0].height<<"\"  "<<student[0].weight<<endl;
    cout<<" Staff "<<staff[0].unique_id<<"  "<<staff[0].height<<"\"  "<<staff[0].weight<<endl;

    return 0;
}

【问题讨论】:

  • 看起来像 C++ 代码。如果是这样,请将 C++ 标签添加到您的问题中。谢谢!
  • 如果你有一个引用int&amp; rx = x;,如果你给像rx = 5;这样的引用分配新值,你认为会发生什么?您认为分配不起作用的那一行发生了什么?
  • 你不能reseat a reference. 这会导致@ArtemyVysotsky 提示的问题。
  • 如果您的教师没有明确禁止使用std::vector,请使用它来代替info* student;info* staff;。顺便说一句,您不必写class info* student;。编译器并不笨。它知道info 是一个类。我建议复习课本的语法部分。但是,如果您在教科书中找到类似的代码,get a better book.

标签: c++ class variable-assignment dynamically-generated


【解决方案1】:

You cannot reseat a reference. 一旦设置,它就会卡在那里,任何重新分配引用的尝试都将被解释为分配给被引用变量的请求。这意味着

person = staff[i];

实际上是将staff[i]; 复制到person 中,这是student[i] 的别名(另一个名称)。 student[i] 将继续接收从用户读取的输入。

考虑到您当前的代码,解决此问题的最简单方法是将引用替换为可以重新定位的指针。

class info* person = &student[i]; // using pointer

int age ;
cout<< "enter age"<<endl;
cin >> age;

if( age > 18 )
{
    person = &staff[i];  // using pointer, but note: nasty bug still here
                         // You may have empty slots in staff 
}

cout<< "enter unique_id"<<endl;
cin >> person->unique_id; // note the change from . to ->
....

但是有一些方法可以解决这个问题。您可以延迟创建引用,直到您知道要使用哪个数组。这需要对您的大量代码进行改组,如果您不小心,仍然会在数组中留下未使用的元素。

幸运的是,使用std::vector from the C++ Standard Library's container library. 有更好的方法来做到这一点

std::vector<info> student;
std::vector<info> staff;

for (int i=0; i<10;i++)
{
    info person; // not a pointer. Not a reference. Just a silly old Automatic

    int age ;
    cout<< "enter age"<<endl;
    cin >> age;

    // gather all of the information in our Automatic variable
    cout<< "enter unique_id"<<endl;
    cin >> person.unique_id;
    cout<< "enter height"<<endl;
    cin >> person.height;
    cout<< "enter weight"<<endl;
    cin >> person.weight;

    // place the person in the correct vector
    if( age > 18 )
    {
        staff.push_back(person);
    }
    else
    {
        student.push_back(person);
    }
}

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2022-06-18
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2017-08-10
    • 2019-04-22
    相关资源
    最近更新 更多