0

.csv ファイルの内容を配列に読み取り、関数getNumberOfRooms()が呼び出されたときに結果を画面 (より大きなプロジェクトの一部) に出力するプログラムを作成しようとしています。値を返そうとすると例外がスローされます。変数のnumberOfRooms、クラス内のプライベート メンバー。誰かが以前にこのような問題を抱えていたり、この種の問題で誰かを助けたりしたことがありますか? もしそうなら、どのように解決しましたか?

前もって感謝します、

完全なソース コードはこちらから入手できます: https://bitbucket.org/skutov/micropuzzle/

getNumberOfRooms() が呼び出されたときにスローされる例外:

Unhandled exception at 0x01354aa6 in MICROPUZZLE.exe: 0xC0000005: Access violation
reading location 0xccccccd0.

これらは問題の関数です(変数は常にクラスで参照されます)

ClassMap::ClassMap ()
{
    numberOfRooms = 0;

    // Get number of rooms in map.csv

    /* Find number of entries in map.csv file */

        numberOfRooms = number_of_lines;

    // allocate memory for rooms array

    /* loading data from file into array */
    }
}    

// self explanitory
int ClassMap::getNumberOfRooms()
{
    // Exception occurs on this line when accessing the variable
    return numberOfRooms;
}

int ClassMap::printRoomDescriptions ()
{
    for(int j = this->getNumberOfRooms(); j > 0; j--)
    {
        cout << roomArray[j].getDescription();
    }
    return 0;
}

クラスヘッダーは次のとおりです。

class ClassMap
{
private:
    int currentLocation;
    int numberOfRooms;
    // pointer to array initialised in constructor
    ClassRoom *roomArray;

public:
    // Constructors and Destructors
    ClassMap();
    ~ClassMap();

    // Print description, events and directions for current room
    std::string getCurrentRoom();

    // Change currentLocation to neighbour of current room if possible
    int moveRoom(char direction);


    // self explanitory
    int getNumberOfRooms();

    // dump room descriptions to command line (debugging)
    int printRoomDescriptions();

};

roomArray も初期化する ClassMap のコンストラクタを次に示します。

ClassMap::ClassMap ()
{
    numberOfRooms = 0;

    // Get number of rooms in map.csv

        unsigned int number_of_lines = 0;
        FILE *infile = fopen("map.csv", "r");
        int ch;

        while (EOF != (ch=getc(infile)))
            if ('\n' == ch)
                ++number_of_lines;
        fclose(infile);
        numberOfRooms = number_of_lines;




    // allocate memory for rooms array
    roomArray = new ClassRoom[numberOfRooms+1];

    // set starting room
    int currentLocation = 1;

    // load that shit up
    {
        // Holders for values read from file
        int newRoomID = 0;
        char newRoomDescription[79] = "";
        int newRoomNorthNeighbour = 0;
        int newRoomEastNeighbour = 0;
        int newRoomSouthNeighbour = 0;
        int newRoomWestNeighbour = 0;

        // used for iterations
        int i = 0;

        // File stream for map.csv
        std::ifstream mapFile;

        // Crack that shit open
        mapFile.open ("map.csv");

        // Line buffer for parsing
        std::string line;


        // For each line in the map.csv file read in the values into variables declared above then run initialise function for each room to store values into array
        while (std::getline(mapFile, line))
        {
            // re-init parameters

            newRoomID = 0;
            newRoomNorthNeighbour = 0;
            newRoomEastNeighbour = 0;
            newRoomSouthNeighbour = 0;
            newRoomWestNeighbour = 0;
            for(i = 0;i<79;i++)
            {
                newRoomDescription[i] = ' ';
            }


            int parameter = 0;

            int paraStart = 0;
            int paraEnd = 0;

            std::string buffer;
            std::istringstream iss(line);

            for(parameter = 0; parameter <= 5; parameter++)
            {
                // Empty buffer from last iteration
                buffer.clear();

                // Find end of current parameter
                paraEnd = line.find(',',paraStart+1);

                switch (parameter)
                {
                case 0:
                    buffer = line.substr((paraStart),(paraEnd-paraStart));
                    newRoomID = atoi(buffer.c_str());
                    break;
                case 1:
                    buffer = line.substr((paraStart+2),(line.find("\"",paraStart+2)-(paraStart+2)));
                    for(i = 0;i<(buffer.length());i++)
                    {
                        newRoomDescription[i] = buffer.c_str()[i];
                    }
                    //newRoomDescription
                    break;
                case 2:
                    buffer = line.substr((paraStart+1),(paraEnd-paraStart));
                    newRoomNorthNeighbour = atoi(buffer.c_str());
                    break;
                case 3:
                    buffer = line.substr((paraStart+1),(paraEnd-paraStart));
                    newRoomEastNeighbour = atoi(buffer.c_str());
                    break;
                case 4:
                    buffer = line.substr((paraStart+1),(paraEnd-paraStart));
                    newRoomSouthNeighbour = atoi(buffer.c_str());
                    break;
                case 5:
                    buffer = line.substr((paraStart+1),(paraEnd-paraStart));
                    newRoomWestNeighbour = atoi(buffer.c_str());
                    break;
                } // switch

                // Cycle paraEnd to paraStart
                paraStart = paraEnd;

            } // for parameters loop

            // Init next room with data
            new (&roomArray[newRoomID]) ClassRoom(  newRoomNorthNeighbour,
                newRoomEastNeighbour,
                newRoomSouthNeighbour,
                newRoomWestNeighbour,
                newRoomDescription);

        } // while !EOF
        // Close the file because we're a good little program and we don't need that shit no more
        mapFile.close();
    }
}
4

2 に答える 2

2

この問題の鍵は次のとおりです。

Access violation reading location 0xccccccd0

0xccccccccデバッグモードで使用される特別な値で、初期化されたポインターを示します。( 0xCCCCCCCC へのポインターで終了する方法を参照してください) この種のクラッシュを引き起こすためにデバッグモードに設定されています - これは、使用しているポインターがまだセットアップされていないことを意味します。ポインターを正しく設定すると、エラーはなくなります。( とのわずかな違い0xccccccccは、そのオブジェクト内でアクセスしようとしているメンバーのオフセットです。)

追加した:

これはあなたのエラーです:

ClassRoom* roomArray = static_cast<ClassRoom*>( ::operator new ( sizeof ClassRoom * numberOfRooms ) );

これにより、ローカルroomArray変数が作成され、メンバー変数が非表示になります。あなたが本当に欲しいのは:

roomArray = static_cast<ClassRoom*>( ::operator new ( sizeof ClassRoom * numberOfRooms ) );

またはさらに良い:

roomArray = new ClassRoom[numberOfRooms];
于 2014-04-08T08:15:22.977 に答える
1

問題は for loop だと思いますfor(int j = this->getNumberOfRooms(); j > 0; j--)。次のようになりますfor(int j = this->getNumberOfRooms()-1; j >= 0; j--)。N 個のエントリを持つ配列の最後のアクセス可能なインデックスは N-1 です。一方、最初のインデックスは 0 です。

于 2014-04-08T08:09:38.020 に答える