如何在标头中声明后初始化2D char数组

时间:2014-02-16 14:39:44

标签: c++ arrays multidimensional-array compiler-errors syntax-error

我正在开展一个项目,需要从2D数组中读取单个字符,以确定相应网格中的内容。

我正在声明2D角色数组如下:

class GridManager{
    static char level1[5][6];
    static char level2[5][6];
    static char level3[6][10];
};

我正在cpp文件中启动它们,如下所示:

char GridManager::level1[5][6] =
{{'O','P','T','T','E','N'},
    {'W','M','N','W','M','P'},
    {'M','T','P','M','P','R'},
    {'O','N','M','P','M','R'},
    {'L','E','E','E','E','N'}};
char GridManager::level2[5][6] = 
{{'O','E','E','E','T','P'},
    {'W','M','B','K','W','R'},
    {'L','B','P','B','M','R'},
    {'M','P','N','T','E','N'},
    {'L','N','B','B','B','R'}};
char GridManager::level3[6][10]= 
{{'O','T','T','T','T','T','T','T','P','R'}
 {'O','T','T','T','T','B','P','R','R','R'}
 {'L','O','T','T','P','P','N','R','P','R'}
 {'W','W','T','L','K','K','K','K','R','R'}
 {'W','W','R','K','T','E','E','O','P','R'}
 {'W','B','N','N','B','B','B','N','B','N'}};

然而,它一直在第一行{在第二行说它期待a}引发错误。 我已经尝试将它们初始化为.cpp文件中的静态,但这没有什么区别。 任何和所有的帮助将不胜感激。 如果您需要更多信息/代码,请不要犹豫 问候, AzKai

2 个答案:

答案 0 :(得分:1)

这看起来不错,但您在最后一个数组的定义中缺少逗号,如下所示:

char GridManager::level3[6][10]= 
{{'O','T','T','T','T','T','T','T','P','R'}, // <-
 {'O','T','T','T','T','B','P','R','R','R'}, // <-
 {'L','O','T','T','P','P','N','R','P','R'}, // <- HERE
 {'W','W','T','L','K','K','K','K','R','R'}, // <-
 {'W','W','R','K','T','E','E','O','P','R'}, // <-
 {'W','B','N','N','B','B','B','N','B','N'}};

答案 1 :(得分:1)

您忘记在定义中放置逗号

char GridManager::level3[6][10]= 
{{'O','T','T','T','T','T','T','T','P','R'}
 {'O','T','T','T','T','B','P','R','R','R'}
 {'L','O','T','T','P','P','N','R','P','R'}
 {'W','W','T','L','K','K','K','K','R','R'}
 {'W','W','R','K','T','E','E','O','P','R'}
 {'W','B','N','N','B','B','B','N','B','N'}};

应该

char GridManager::level3[6][10]= 
{{'O','T','T','T','T','T','T','T','P','R'},
 {'O','T','T','T','T','B','P','R','R','R'},
 {'L','O','T','T','P','P','N','R','P','R'},
 {'W','W','T','L','K','K','K','K','R','R'},
 {'W','W','R','K','T','E','E','O','P','R'},
 {'W','B','N','N','B','B','B','N','B','N'}};