C++ 使用映射类型会创建gcc错误:在‘;之前应为非限定id;对于’;

C++ 使用映射类型会创建gcc错误:在‘;之前应为非限定id;对于’;,c++,gcc,map,bitset,C++,Gcc,Map,Bitset,我是linux新手,在Ubuntu Virtual box环境中使用终端。我无法找出这些错误发生的原因和原因,而且它们似乎与缺少的“;”不匹配或者定义冲突 以下是我从gcc编译器中得到的错误: my_predictor.h:82:42: error: template argument 2 is invalid my_predictor.h:82:42: error: template argument 4 is invalid my_predictor.h:84:9: error: expec

我是linux新手,在Ubuntu Virtual box环境中使用终端。我无法找出这些错误发生的原因和原因,而且它们似乎与缺少的“;”不匹配或者定义冲突

以下是我从gcc编译器中得到的错误:

my_predictor.h:82:42: error: template argument 2 is invalid
my_predictor.h:82:42: error: template argument 4 is invalid
my_predictor.h:84:9: error: expected unqualified-id before ‘for’
my_predictor.h:84:23: error: ‘g’ does not name a type
my_predictor.h:84:44: error: ‘g’ does not name a type
my_predictor.h:91:42: error: template argument 2 is invalid
my_predictor.h:91:42: error: template argument 4 is invalid
my_predictor.h:93:9: error: expected unqualified-id before ‘for’
my_predictor.h:93:23: error: ‘f’ does not name a type
my_predictor.h:93:39: error: ‘f’ does not name a type
my_predictor.h: In member function ‘virtual branch_update* local_predictor::predict(branch_info&)’:
my_predictor.h:107:57: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:111:40: error: invalid types ‘int[int]’ for array subscript
my_predictor.h: In member function ‘virtual void local_predictor::update(branch_update*, bool, unsigned int)’:
my_predictor.h:121:62: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:124:46: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:125:33: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:127:41: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:128:29: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:131:33: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:132:30: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:135:33: error: invalid types ‘int[int]’ for array subscript
my_predictor.h:136:30: error: invalid types ‘int[int]’ for array subscript
我认为关键问题在于使用索引键声明和初始化Map构造,然后是一个位集对象,它是一个位字符串。它似乎在数组[]操作中创建了后来的错误(根据我在cplusplus.com/reference/map site上的发现,这些操作应该是有效的)

这是我的密码:

1:// my_predictor.h
// This file contains a sample gshare_predictor class.
// It is a simple 32,768-entry gshare with a history length of 15.

5:#include <bitset>
#include <map>
7:using namespace std;

64:class local_predictor : public branch_predictor {
65:public:
#define LHBITLEN 10
#define PREDCNTR 2
#define LOCHISTTABLERNG 4096
#define LOCPREDRNG 1024
70:     local_update u;
        branch_info bcopy;

        // otherwise ints, where each will be multiplied by 10, then add 1 if taken/true. Initial test case to change if 10 bits long already

75:     // ?correct location? Bit array of length 10 for local history table value entries
        bitset<LHBITLEN> lhthistval; 
        bitset<PREDCNTR> lpcounter;

        // initialize to 0's 
80:       // lhthistval.reset();

        std::map<int, (bitset<LHBITLEN>) > lochisttab;
        //map<int, (bitset<10>) > lht;
        for (int g=0; g < LOCHISTTABLERNG; g++) {
85:            //const int j = g;
            lochisttab[g] = lhthistval.reset();
            //lht.insert(pair<int, (bitset<LHBITLEN>)>(j, lhthistval.reset()));
        }

90:
    std::map<int, (bitset<PREDCNTR>) > locprediction;
    //map<int, (bitset<2>) > locpredict;
    for (int f=0; f < LOCPREDRNG; f++) {
        //const int j = f;
95:        //locpredict.insert(pair<int, (bitset<PREDCNTR>)>(j,lpcounter.reset()));
        locprediction[f] = lpcounter.reset();
    }


100:    local_predictor (void) {
    }

    branch_update *predict (branch_info & b) {
        bcopy = b;
105:        // address for locpredict based on value by modulus (remainder) of branch address divide by 4096
        int braddr = static_cast<int>(b.address % LOCHISTTABLERNG);
        bitset<LHBITLEN> address = lochisttab[braddr];
        // safe and compiler allow since it is only 10 bits long max?
        int indx = static_cast<int>(address.to_ulong());
110:        // use the MSB or Pos 1 in 2 bit array to set boolean Take/Don't
        bool take = locprediction[indx].test(1);
        u.direction_prediction (take);
        u.target_prediction (0);
        return &u;
115:    }

    void update (branch_update *u, bool taken, unsigned int target) {

        if (bcopy.br_flags & BR_CONDITIONAL) {
120:            int lhtaddress = bcopy.address % LOCHISTTABLERNG;
            bitset<LHBITLEN> addr = lochisttab[lhtaddress];
            int indx = static_cast<int>(addr.to_ulong());
            for (int i=1; i < LHBITLEN; i++) {
                bool prval = lochisttab[lhtaddress].test(i-1);
125:                lochisttab[lhtaddress].set(i,prval);
            }
            bool prev = locprediction[indx].test(0);
            locprediction[indx].set(1,prev);

130:            if (taken) {
                lochisttab[lhtaddress].set(0);
                locprediction[indx].set(0);
            }
            else {
135:                lochisttab[lhtaddress].reset(0);
                locprediction[indx].reset(0);
            }
        }
    }

};
1://my_predictor.h
//此文件包含一个示例gshare_预测器类。
//它是一个简单的32768条目gshare,历史长度为15。
5:#包括
#包括
7:使用名称空间标准;
64:类本地\u预测器:公共分支\u预测器{
65:公众:
#定义LHBITLEN 10
#定义PREDCNTR 2
#定义LOCHISTTABLERNG 4096
#定义LOCPREDRNG 1024
70:本地更新;
分行信息复印件;
//否则为int,其中每个将乘以10,如果为take/true,则加1。如果已经有10位长,则更改初始测试用例
75://?正确位置?长度为10的位数组,用于本地历史记录表值项
位集lhthistval;
位集计数器;
//初始化为0
80://lhthistval.reset();
std::map lochisttab;
//地图lht;
对于(int g=0;g

编辑:我最初对映射声明的位集参数没有疑义。编译器给出了第一个错误“在'for'之前应为非限定id”。模板参数错误来自于偏执论(但我认为这是最终理解位集数据类型的结果)。

我猜
(位集)周围的假括号
第82行是问题所在——它们可能会导致编译器尝试将其作为模板值参数而不是类型参数进行解析,然后在尝试恢复时感到困惑


像这样一系列错误的情况一样,只有第一个错误是相关的,并告诉您问题是什么。后面的问题是由于编译器在试图恢复有意义的上下文以继续运行时丢弃了太多的标记,对它所看到的内容感到困惑。它可能扔掉了
时,认为它仍在尝试解析模板参数列表这是您需要的语法:

`map<int, bitset<10> > intBitsetMap;  //Note the space between the two: > >`
`map intBitsetMap//请注意两者之间的空格:>>`

既然您提到了这一点:有没有办法告诉g++在出现第一个错误后停止?有时,特别是在处理元类型、sfinae、enable_if等时。编译器消息大约有50行,结尾很难看到……谢谢,我编辑以反映我在第一次得到关于“for”标识的错误后添加了偏执。其他错误“error:'g'未命名类型”最初也仍然存在。但是“int[int]”错误没有出现。您不能将类型放在括号中。您似乎在任何函数之外都有
for
语句。我添加了parantises实际上是为了响应第一次出现的错误“error:expected unqualified id before'for'”。parantheses创建了无效参数的较新错误。@n.m.标头类声明中不允许进行for循环初始化吗?我原来在>>之间有一个空格,并使用了#define for a numeral value-将#define变量替换为实际数字有意义吗?