1

まず、これはクラス用なので、できることとできないことに制限があります。さらに、私は c++ とプログラミング全般に非常に慣れていないため、コードはおそらく少しがらくたです。

最初の for ループ内で cout 行の最初のセットを使用して item_list を表示すると、個々のアイテムが本来あるべき状態で表示される理由を理解しようとしています (スカイリムの成分とその効果のリスト)。

However, when the second for loop executes, the item_list is filled with nothing but the last item that should have been inserted (wisp wrappings and their effects).

Even just pointing me in the right direction would be GREATLY appreciated :)

cheers

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

... Header for the table class

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

.... Beginning of main

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

....

item header

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

1 に答える 1

4

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のメモリと同じメモリを指すitem_listポインタ (など) があります。 nameclient_item

たとえば、各割り当ての後、ポインタitem_list[index].nameとポインタitem_in.nameは同じ値になります。(興味がある場合は、両方のポインタを出力して確認できます。) どちらも同じメモリを指しているため、そのメモリに保存されているものを変更すると、両方のオブジェクトが同時に変更されたように見えます。

つまりclient_item、新しい文字列をそれが指す場所の 1 つにコピーするなど、その後の変更は、保存されたすべてのアイテムにも影響します。

于 2012-05-18T19:07:26.493 に答える