在不使用动态数组的情况下遇到数组初始化问

时间:2013-12-04 10:14:23

标签: c++ arrays initialization

大家好,我希望可能会对我遇到问题的这段代码有所帮助。对不起,但我一直在重写这篇文章寻找解决方案。 所以这是交易,我试图在类a.k.a“mLevelDesign”中硬编码数组。因此,每当我需要更新“地图”时,我都可以将它传递给mMapBuffer。我的问题是,我觉得自己需要更加努力,但我已经尝试创建数组的第三个实例,以便我在标题中从private中删除mLevelDesign并添加说mCopyLevel,然后将mLevelDesign传递给mCopyLevel构建映射,然后在mLevelDesign的位置使用mCopyLevel。虽然我似乎已经失去了很多内存和其他问题。我只是好奇是否有一些简单的方法能够硬编码一个数组,同时仍然保持它在头文件中列出,甚至更好,但没有在内存中丢失它?截至目前,此代码抛出错误:无法在赋值时将'大括号括起初始化列表'转换为'int'。

//Map.h
#ifndef MAP_H
#define MAP_H

class Map
{
public:
    Map();

    void update();
    void drawMap();

private:
    int  mLevelDesign[100];
    int* mMapBuffer;
    int  mMapX;
    int  mMapY;
    int  mPlayerX;
    int  mPlayerY;
    bool mBossDead;
    bool mEnemy1Dead;
    bool mEnemy2Dead;
    bool mTreasureLooted;
};

#endif // MAP_H


//Map.cpp
#include <iostream>
#include "Map.h"

using namespace std;

Map::Map()
{
    mMapX = 10;
    mMapY = 10;

    mPlayerX =  6;
    mPlayerY = 10;

    mLevelDesign[100] =
            /* 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,10 */
    /*1*/    { 0 , 0 , 0 , 0 , 1 ,'D', 1 , 0 , 0 , 0 ,
    /*2*/      0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ,
    /*3*/      1 , 2 , 2 , 2 , 1 , 0 , 1 , 2 , 1 , 0 ,
    /*4*/      1 ,'T','E', 0 , 0 , 0 , 0 ,'F', 1 , 0 ,
    /*5*/      1 ,'B','E', 0 , 0 , 0 , 0 ,'M', 1 , 0 ,
    /*6*/      1 , 2 , 2 , 2 , 1 , 0 , 1 , 2 , 1 , 0 ,
    /*7*/      0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ,
    /*8*/      0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ,
    /*9*/      0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ,
    /*10*/     0 , 0 , 0 , 0 , 1 ,'D', 1 , 0 , 0 , 0 };

    mBossDead       = false;
    mEnemy1Dead     = false;
    mEnemy2Dead     = false;
    mTreasureLooted = false;
}

void Map::update()
{
    //Re-draw buffer from map copy
    mMapBuffer = new int[0];
    int arraySize = mMapX * mMapY;
    for(int i = 0; i < arraySize; ++i)
    {
        mMapBuffer[i] = mLevelDesign[i];
    }

    //Convert player cooordinates to array standard and draw player to map.
    int playerConversion = ((mPlayerY - 1) * mMapY) + (mPlayerX - 1);
    mMapBuffer[playerConversion] = 'P';

    //Check for changes in map.
    if(mBossDead == true)
        mMapBuffer[41] = 0;

    if(mEnemy1Dead == true)
        mMapBuffer[32] = 0;

    if(mEnemy2Dead == true)
        mMapBuffer[42] = 0;
}

void Map::drawMap()
{
    int count =  0;
    for(int y = 0; y < mMapY; ++y)
    {
        for(int x  = 0; x < mMapX; ++x)
        {
            if(mMapBuffer[count] == 0)
                {cout << "  "; count ++;}

            else if(mMapBuffer[count] == 1)
                {cout << "| "; count ++;}

            else if(mMapBuffer[count] == 2)
                {cout << "= "; count ++;}

            else if(mMapBuffer[count] == 'X')
                {cout << "X "; count ++;}

            else if(mMapBuffer[count] == 'D')
                {cout << "D "; count ++;}

            else if(mMapBuffer[count] == 'T')
                {cout << "T "; count ++;}

            else if(mMapBuffer[count] == 'P')
                {cout << "@ "; count ++;}

            else if(mMapBuffer[count] == 'M')
                {cout << "$ "; count ++;}

            else if (mMapBuffer[count] == 'F')
                {cout << "F "; count ++;}

            else if (mMapBuffer[count] == 'Q')
                {cout << "Q "; count ++;}

            else if(mMapBuffer[count] == 'E')
                {cout << "E "; count ++;}

            else if(mMapBuffer[count] == 'B')
                {cout << "B "; count ++;}

            else
                {cout << "  "; count ++;}
        }
        cout << endl;
    }
}

1 个答案:

答案 0 :(得分:1)

你不能这样做。将初始化放在构造函数初始化列表中,或者手动设置字段后的字段,或者创建一个临时数组并将其std::copy添加到实际数组中。

由于尺寸的原因,我会推荐最后一个:

Map::Map()
{
    static const int tempLevel[100] = { ... };

    std::copy(tempLevel, tempLevel + 100, mLevelDesign);

    ...
}