c++二维向量语法错误

c++ two dimensional vector syntax error

本文关键字:向量 语法 错误 二维 c++      更新时间:2023-10-16

有一个common.h文件:

typedef enum {
  num_letters = 26,
  num_directions = 2
} constants;

和另一个类的。h文件:

#include "Common.h"
using namespace common;
... some code ...
    vector< vector<uint> > data(constants::num_directions, vector<uint>(constants::num_letters, 0));

我检查了向量的语法,它应该是正确的;我也在使用c++11 std;然而,它给了我以下错误:

error: 'constants::num_directions' is not a type
     vector< vector<uint> > data(constants::num_directions, vector<uint>(constants::num_letters, 0));
 error: expected unqualified-id before ',' token
     vector< vector<uint> > data(constants::num_directions, vector<uint>(constants::num_letters, 0));
                                                                                               ^
error: expected ')' before ',' token
 error: expected ')' before ',' token
 error: expected unqualified-id before numeric constant
     vector< vector<uint> > data(constants::num_directions, vector<uint>(constants::num_letters, 0));

,我真的不知道为什么会发生,非常感谢你提前!

您没有提供足够的代码来可靠地重现您的问题,因此我将大胆猜测:

从编译器发出的错误消息来看,它似乎将整行解释为函数声明而不是变量定义。这可能是由于这样一个事实,正如在评论中指出的那样,constants::num_directions不是使用普通enum的枚举数的有效方式。所以编译器会像解析函数声明那样解析这一行

ResultType f(ArgType1, Argtype2(argname2))

ResultTyoe是vector<vector<uint>>, ArgType1constants::num_directions,它不识别为类型,ArgType2vector<uint>,然后它完全丢失了,因为它不能使头部或尾部的其余部分。