2011-12-20 125 views
-2

我在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 

在文件散列的散列的定义.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*> {}; 
} 

编辑::我需要包括命名空间。处理他人的代码会导致愚蠢的错误。 感谢您的投票和帮助。 :)

任何帮助表示赞赏。谢谢。

+0

写'哈希(ext_str_tab_s1)'会不会更容易? – Hauleth 2011-12-20 20:14:04

+1

你是否包含定义Hash是什么的东西? – tpg2114 2011-12-20 20:14:12

+1

命名空间中是否包含和/或定义模板类'Hash'?如果是这样,可能是你的问题。 – Joe 2011-12-20 20:15:09

回答

2

至于我可以看到:

  • 你不包括"hash.h"
  • 你注释掉Hash模板的声明,只留下了明确的专业。即使您未定义通用版本,您仍然需要声明该模板。
  • Hash模板位于名称空间Hash中,但您既不使用该名称空间,也不完全限定模板名称。 (顺便说一句,对名称空间和类使用相同的名称并不是一个好主意;它可能导致含糊不清)。
  • 您正试图将非静态的operator()称为静态;你想要更像return Hash<const char*>()(ext_str_tab_s1);的东西。

因此,Hash未被识别为模板名称并不奇怪(虽然必须说错误消息不是很有用)。

+0

@crazy_prog,这是一个很好的答案。而且,你需要退后一点。这段代码来自哪里?它有很多问题。你自己写了吗?它的任何版本有用吗? – 2011-12-20 21:53:41

2

有几个可能的选项:

  • 你不包括其中Hash模板被定义
  • 模板Hash是在不同的命名空间
1

Hash是一个命名空间标题。命名空间不能有模板参数。

不要给类型和名称空间赋予相同的名称。

相关问题