繁体   English   中英

C ++类中的多维数组

[英]Multidimensional array in class C++

使用多维数组编译代码时出现错误。

C:\\ Users \\ Tobi13 \\ Desktop \\ Projet C ++ \\ Code \\ Test \\ Test111 \\ map.hpp | 17 |错误:作为多维数组的“ levelchargement”声明必须具有除第一个|

我一点也不明白。 我已经在互联网上搜索了,但没有找到任何东西。

map.hpp的代码

class Map{
    public:
        void        ChargementFichiersMap();
        void        ChargementFichierElements();
static  int         widthmap, heightmap;
        int         floorx, floory, floorwidth, floorheight;
        int         wallx, wally, wallwidth, wallheight;
        int         tpx, tpy, tpwidth, tpheight;
        char        *levelchargement[][]= new char [widthmap][heightmap];
        sf::IntRect wallrect, floorrect, tprect;
        sf::Texture tileset;

};

map.cpp

void Map::ChargementFichiersMap()
{
    ifstream fichiermap("map1.txt", ios::in);
    fichiermap >> widthmap >> heightmap;
    fichiermap.ignore();
    char lecture;
    int i=0;
    int j=0;
    while(fichiermap.get(lecture))
    {
        if(lecture==0x0A) // 0x0A est le code hexa pour le saut de ligne
        {
            j++;
            i=0;
            cout << "\n";
        }
        else
        {
            levelchargement[i][j]=lecture;
            i++;
            cout << lecture;
        }
    }
    fichiermap.close();

    ifstream fichiernommap("nom_map.txt", ios::in);
    char nommap;
    fichiernommap >> nommap;
    tileset.loadFromFile(nommap);
    fichiernommap.close();

}

void Map::ChargementFichierElements()
{
    ifstream fichierfloor("floor.txt", ios::in);
    fichierfloor >> floorx >> floory >> floorwidth >> floorheight;
    floorrect(floorx, floory, floorwidth, floorheight);
    fichierfloor.close();

    ifstream fichierwall("wall.txt", ios::in);
    fichierwall >> wallx >> wally >> wallwidth >> wallheight;
    wallrect(wallx, wally, wallwidth, wallheight);
    fichierwall.close();

    ifstream fichiertp("tp.txt", ios::in);
    fichiertp >> tpx >> tpy >> tpwidth >> tpheight;
    tprect(tpx, tpy, tpwidth, tpheight);
    fichiertp.close();
}

main.cpp

int main()
{
    sf::RenderWindow window(sf::VideoMode(720,480), "SFML works?");
    Map test;
    test.ChargementFichiersMap();
    test.ChargementFichierElements();
    while (window.isOpen())
    {
            sf::Vector2f taille(32.f,32.f);
            for (float y = 0; y < test.heightmap; y++)
            {
                for (float x = 0; x < test.widthmap; x++)
                {
                    switch (test.*(levelchargement[int(x)][int(y)]))
                    {
                        case 'W':
                            {
                                sf::RectangleShape wall(taille);
                                wall.setTexture(&test.tileset);
                                wall.setTextureRect(test.wallrect);
                                wall.setPosition(x*32.f, y*32.f);
                                window.draw(wall);
                                break;
                            }

                         case ' ':
                             {
                                sf::RectangleShape floor(taille);
                                floor.setTexture(&test.tileset);
                                floor.setTextureRect(test.floorrect);
                                floor.setPosition(x*32.f, y*32.f);
                                window.draw(floor);
                                break;
                             }

                        case 'T':
                             {
                                sf::RectangleShape tp(taille);
                                tp.setTexture(&test.tileset);
                                tp.setTextureRect(test.tprect);
                                tp.setPosition(x*32.f, y*32.f);
                                window.draw(tp);
                                break;
                             }
                         default:
                            {
                                sf::RectangleShape defaut(taille);
                                defaut.setFillColor(sf::Color::Blue);
                                defaut.setPosition(x*32.f, y*32.f);
                                window.draw(defaut);
                                break;
                            }
                    }
                }
            }
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.display();
    }

    return 0;
}

如果不带任何类和所有类,则整个代码都可以正常工作,但是现在我必须以面向对象的编程方式进行操作。 希望你能帮我,祝你有美好的一天!

char *levelchargement[][]= new char [widthmap][heightmap]; 对多维数组不是有效的声明或分配。 由于要在运行时动态分配数组,因此需要将分配移到类构造函数中,并使用以下语法:

class Map{
    public:
        Map();
        Map(const Map&);
        ~Map();
        ...
        char        **levelchargement;
        ...
};

Map::Map()
{
    ...
    levelchargement = new char*[widthmap];
    for (int i = 0; i < widthmap; ++i)
        levelchargement[i] = new char[heightmap];
    ...
}

Map::Map(const Map &src)
{
    ...
    levelchargement = new char*[widthmap];
    for (int i = 0; i < widthmap; ++i)
        levelchargement[i] = new char[heightmap];
    ...
}

Map::~Map()
{
    for (int i = 0; i < widthmap; ++i)
        delete[] levelchargement[i];
    delete[] levelchargement;
}

暂无
暂无

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

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