使用地图类型创建gcc错误:'for'之前的预期unqualified-id

时间:2013-04-18 22:52:25

标签: c++ gcc map bitset

我是linux新手,在Ubuntu虚拟机环境中使用终端。我无法弄清楚这些错误发生的原因和原因,并且它们似乎与缺少的“;”相匹配。或#define冲突。

以下是我从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构造,然后是一个bitset对象,它是一串位。它似乎使用array []操作创建了后来的错误(它应该是我在cplusplus.com/reference/map站点上找到的有效)。

这是我的代码:

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);
            }
        }
    }

};

编辑:我最初没有围绕地图声明的bitset参数的parantheses。编译器给出了第一个错误“在'之前的'期望的非限定id'。模板参数错误来自parantheses(但我认为这是结果,因为最终可能会理解bitset数据类型)。

2 个答案:

答案 0 :(得分:1)

我猜第82行(bitset<LHBITLEN>)周围的虚假括号是问题 - 它们可能会导致编译器尝试将其解析为模板值参数而不是类型参数,然后混淆,因为它试图恢复。

像这样的一连串错误通常就是这种情况,只有第一个是相关的,并告诉你问题是什么。后者是由于编译器抛弃了太多的令牌,因为它试图恢复有意义的上下文以继续,让人对它正在看的内容感到困惑。它可能会丢弃>;并认为它仍然在尝试解析模板参数列表时看到for

答案 1 :(得分:0)

这是您需要的语法:

`map<int, bitset<10> > intBitsetMap;  //Note the space between the two: > >`