简体   繁体   English

C ++向数组添加元素

[英]C++ adding elements to an array

this is a part of an assignment for my programming class. 这是我的编程课作业的一部分。 the teacher wanted us to create a couple of functions, one of which would add elements to an existing dynamic array of structures, and this is what I have troubles with. 老师希望我们创建几个功能,其中一个功能是将元素添加到现有的动态结构数组中,而这正是我遇到的麻烦。

here's my understanding of how the function should work, based on different posts I found online: 根据我在网上发现的不同信息,这是我对该功能的工作方式的理解:

  1. create a new array, bigger than the one already existing 创建一个新的数组,大于现有数组

  2. copy the content of the old array to the new array 将旧数组的内容复制到新数组

  3. add the new element to the new array 将新元素添加到新数组

  4. destroy the old array 销毁旧数组

however, something is wrong, and the program crashes - I think the problem lies in the way I'm trying to do points 3 and 4. Can someone take a look? 但是,出了点问题,程序崩溃了-我认为问题出在我尝试执行第3点和第4点的方式上。有人可以看看吗? I'd really appreciate any kind of help. 我真的很感谢任何帮助。

edit: forgot to mention, the teacher wants the functions set to void, they are supposed to not return anything. 编辑:忘了提及,老师希望将功能设置为void,它们应该不返回任何内容。

Here is the code: 这是代码:

const int size = 2;

struct Player {
    string name;
    string kind;
};

void addplayer(Player * plarr, int size) {

    cout << "Adding a new element to the array" << endl << endl;

    //creating a new, bigger array:
    Player * temp = NULL;
    temp = new Player[size+1];

    //copying the content of the old array
    for (int i=0;i<size;i++) {
        temp[i].name = plarr[i].name;
        temp[i].kind = plarr[i].kind;   
    }

    //adding the new element:
    string name, kind;
    cout << "Choose the name for the new player: " << endl;
    cin >> name;
    cout << "Choose the class for the new player: " << endl;
    cin >> kind;

    temp[size+1].name = name;
    temp[size+1].kind = kind;

    //deleting the old array, replacing it with the new one
    delete[] plarr;     
    plarr = temp; 

}
void addplayer(Player * plarr, int size) {

The plarr parameter is passed to this function by value . plarr参数通过value传递给此函数。

This function appears to allocate a new array and copy over the contents correctly, except for one error: 此函数似乎分配一个新数组并正确复制内容,但有一个错误除外:

temp[size+1].name = name;
temp[size+1].kind = kind;

The index should be size , here. 索引应该是size But the biggest error is that the function concludes with: 但是最大的错误是该函数以以下结尾:

    delete[] plarr;     
    plarr = temp; 
}

Unfortunately, since plarr was passed by value, all this does is set the plarr parameter to this function to the new pointer, and returns. 不幸的是,由于plarr是按值传递的,因此所有操作都是将此函数的plarr参数设置为新指针,然后返回。

Which accomplishes absolutely nothing, since the caller to this function still has its original pointer. 这绝对不会完成任何事情,因为此函数的调用者仍具有其原始指针。 Which is now destroyed. 现在已被销毁。

You should change this function to return the new pointer, which the caller will need to save, instead of the original pointer. 您应该更改此函数以return调用者需要保存的新指针,而不是原始指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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