c++ - 无法填充 C++ 数组,每个索引处只有最后一项

标签 c++ arrays for-loop ifstream cout

首先,这是针对一个类的,所以我们能做什么和不能做什么都有限制,而且我对 C++ 和一般编程都非常陌生,所以这就是代码可能有点乱的原因。

我无能为力地试图理解为什么当我在第一个 for 循环中使用第一组 cout 行显示 item_list 时,它会按应有的方式显示每个单独的项目(它是天际成分及其效果的列表).

但是,当第二个 for 循环执行时,item_list 只填充了本应插入的最后一项(wisp 包装及其效果)。

即使只是为我指出正确的方向,我也会非常感激 :)

干杯

int client::fill_list(int size_in, int h1size, int h2size)
{
    char temp[ASIZE] = {'\0'};
    int j = 0;
    ifstream ifile;
    ifile.open("test.txt");

    if(ifile.is_open())
    {
        for(int i = 0; i < size_in; ++i)
        {    
            if(ifile.good())
            {       
                j = 0;
                do
                {                   
                    temp[j] = char(ifile.get());
                    ++j;
                }while(ifile.peek() != '*');
                temp[j] = char(ifile.get());
                copy(client_item.name, temp, j);
            }


            if(ifile.good())
            {
                j = 0;
                do
                {                   
                    temp[j] = char(ifile.get());
                    ++j;
                }while(ifile.peek() != '*');
                temp[j] = char(ifile.get());
                copy(client_item.effect1, temp, j);
            }

            if(ifile.good())
            {
                j = 0;
                do
                {                   
                    temp[j] = char(ifile.get());
                    ++j;
                }while(ifile.peek() != '*');
                temp[j] = char(ifile.get());
                copy(client_item.effect2, temp, j);
            }

            if(ifile.good())
            {
                j = 0;
                do
                {                   
                    temp[j] = char(ifile.get());
                    ++j;
                }while(ifile.peek() != '*');
                temp[j] = char(ifile.get());
                copy(client_item.effect3, temp, j);
            }

            if(ifile.good())
            {
                j = 0;
                do
                {                   
                    temp[j] = char(ifile.get());
                    ++j;
                }while(ifile.peek() != '*');
                temp[j] = char(ifile.get());
                copy(client_item.effect4, temp, j);
            }
            reference.into_list(i,client_item);
        cout << reference.item_list[i].name;
        cout << reference.item_list[i].effect1;
        cout << reference.item_list[i].effect2;
        cout << reference.item_list[i].effect3;
        cout << reference.item_list[i].effect4;
        getchar();
        }
    }
    for(int k = 0; k < SIZE; ++k)
    {
        cout << reference.item_list[k].name;
        cout << reference.item_list[k].effect1;
        cout << reference.item_list[k].effect2;
        cout << reference.item_list[k].effect3;
        cout << reference.item_list[k].effect4;
    }
    getchar();
    return 0;
}

...

int table::into_list(int index, item&item_in)
{
    if(index < SIZE)
    {
        item_list[index] = item_in;
        return 0;
    }
    else
        return 1;
}

... 表类的标题

#include "hash.h"

class table
{
public:
    table()
    {
        item_list = new item [SIZE];
    }
    ~table();
    int fill(item*);
    int insert(item&, nHash&);
    int insert(item&, eHash&, int);
    int retrieve(char*,item*,int);
    int remove(int,item&);
    int remove(int);
    int check_hash(int,int,int);
    int keygen(char*, int);
    int from_list(int, item&);
    int into_list(int, item&);

//private:
    item * item_list;
    nHash name_table;
    eHash ef1_table;
    eHash ef2_table;
    eHash ef3_table;
    eHash ef4_table;
};

.... 主线开始

#include "client.h"

int main()
{
    client program;

    program.fill_list(SIZE,HNSIZE,HESIZE);

    for(int i = 0; i < SIZE; ++i)
    {
        cout << program.reference.item_list[i].name;
        cout << program.reference.item_list[i].effect1 << endl;
        cout << program.reference.item_list[i].effect2 << endl;
        cout << program.reference.item_list[i].effect3 << endl;
        cout << program.reference.item_list[i].effect4 << endl;
    }

....

项目标题

#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

const int ASIZE = 30;
const int SIZE = 92;
const int HNSIZE = 41;
const int HESIZE = 17;

struct item
{
    item();
    ~item();
    char * name;
    char * effect1;
    char * effect2;
    char * effect3;
    char * effect4;
    int count;
    //int keygen(int,int);
    /*int name_key;
    int ef1_key;
    int ef2_key;
    int ef3_key;
    int ef4_key;*/
};

最佳答案

部分问题可能在于您如何在此处制作 client_item 的拷贝:

reference.into_list(i,client_item);

这只是将 client_item 分配给 item_list,如下所示:

item_list[index] = item_in;

...但是因为 item 是这样定义的:

struct item
{
    item();
    ~item();
    char * name;
    char * effect1;
    ...

...item_list 中的所有 item 都有指向同一内存的指针(如 name 等)作为 client_item 中的内存。

例如,每次赋值后,指针item_list[index].nameitem_in.name 将具有相同的值。 (如果您好奇的话,可以通过打印两个指针来检查这一点。)由于它们都指向同一内存,如果您更改存储在该内存中的内容,两个对象将同时发生更改。

这意味着对 client_item 的后续更改(例如将新字符串复制到它指向的位置之一)也会影响所有已保存的项目。

关于c++ - 无法填充 C++ 数组,每个索引处只有最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658035/

相关文章:

c - 如何在 C 中找到 const 声明的数组的大小?

c++ - 向开源项目学习/浏览已经编写的代码

c++ - 如何将 Ubuntu C++ 库移植到 MinGW?

python - 如何使用 matplotlib/numpy 将数组保存为灰度图像?

c++ - 泛化多个嵌套的 for 循环

c - 为什么分号不清空循环体?

for-loop - 如何获取 apex for 循环中的键和值?

c++ - 圆柱纹理映射opengl

c++ - 如何在 C 或 C++ 中获取主板地址或处理器 ID/序列号?

c++ - 有没有办法声明一定大小的 std::vector 数组?