我在linux上使用gcc4.4.1编译了以下代码:-
#include "glob.h"
#include "netlist.h"
#include "netlist_params.h"
#include "netlist_abbrev.h"
#include "lvs_util.h"
#include "lvs_report.h"
#include "lvs_data.h"
#include "compare_opts.h"
#include "flatten.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include "hash.h"
static THREAD_PRIVATE NlStringVec* ext_str_tab_v;
static THREAD_PRIVATE const char* ext_str_tab_s1;
static THREAD_PRIVATE const char* ext_str_tab_s2;
int Netlist::ExtStrHash::operator () (NlString i) const {
if (i.is_valid())
ext_str_tab_s1 = (*ext_str_tab_v)[i];
return Hash<const char*>::operator()(ext_str_tab_s1);
}
错误:-
netlist_back_1.C: In member function 'int Netlist::ExtStrHash::operator()(NlString) const':
netlist_back_1.C:24: error: expected primary-expression before '<' token
netlist_back_1.C:24: error: expected primary-expression before 'const'
netlist_back_1.C:24: error: expected ';' before 'const'
netlist_back_1.C:24: error: expected unqualified-id before '>' token
netlist_back_1.C:24: error: expected initializer before '>' token
hash.h 文件中哈希的定义:-
namespace Hash {
// template <class Key> struct Hash { };
#define DECL_SIMPLE_HASH(type) \
template <> \
struct Hash<type> { \
unsigned int operator() (type x) const { return x; } \
}
DECL_SIMPLE_HASH(signed char);
DECL_SIMPLE_HASH(unsigned char);
DECL_SIMPLE_HASH(signed short);
DECL_SIMPLE_HASH(unsigned short);
DECL_SIMPLE_HASH(signed int);
DECL_SIMPLE_HASH(unsigned int);
DECL_SIMPLE_HASH(signed long);
DECL_SIMPLE_HASH(unsigned long);
DECL_SIMPLE_HASH(signed long long);
DECL_SIMPLE_HASH(unsigned long long);
#undef DECL_SIMPLE_HASH
template <>
class Hash<const char*> {
static const int M = 61; // 5;
public:
unsigned int operator() (const char* s) const {
// case insensitive, so sensitivity can be turned on/off without
// effecting hash #.
unsigned h = 0;
char c;
while (c = *s++) {
if (c >= 'A' && c <= 'Z')
c = c - 'A' + 'a';
h = M*h + c;
}
return h;
}
};
template <> struct Hash<char*> : public Hash<const char*> {};
}
编辑::我需要包含 namespace 。使用别人的代码会导致愚蠢的错误。 感谢您的否决和帮助。 :)
感谢任何帮助。谢谢。
最佳答案
据我所知:
- 您没有包括
"hash.h"
- 您已注释掉
Hash
的声明模板,只留下明确的特化。即使您没有定义通用版本,您仍然需要声明模板。 Hash
模板位于命名空间Hash
内,但您既不使用该命名空间,也不完全限定模板名称。 (顺便说一句,对命名空间和类使用相同的名称不是一个好主意;它可能会导致歧义)。- 您正在尝试调用非静态
operator()
就好像它是静态的;你想要更像return Hash<const char*>()(ext_str_tab_s1);
的东西.
因此,Hash
也就不足为奇了。不被识别为模板名称(尽管不得不说错误消息没有多大帮助)。
关于c++ - 错误: expected primary-expression before '<' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8581304/