我是 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 对象,它是一串位。似乎使用数组 [] 操作创建了后来的错误(从我在 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);
}
}
}
};
编辑:我最初对 map 声明的 bitset 参数没有任何附加条件。编译器给出了第一个错误“'for'之前的预期不合格ID”。模板参数错误来自括号(但我认为这是结果,因为最终可能理解了 bitset 数据类型)。