c++ - 错误: expected primary-expression before '<' token

标签 c++ linux

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

相关文章:

c++ - 代码分析说 Inconsistent annotation for 'wWinMain' : this instance has no annotations

c++ - 如何从复杂算法中提取事件代码路径

c++ - 从句子中获取单词并将它们存储在字符串 vector 中

c++ - Qt5 支持的应用程序使用 Qt4-libs 并失败。如何避免这种情况?

c++ libssh - ssh_channel_read() 一直返回 0 而 ssh_channel_is_open() 返回 false

linux - shell 脚本中的 while 循环在 linux bash shell 中不起作用

java - 如何存储类属性/字段

c++ - 是否有任何广泛使用的支持测试类别的 C++ 单元测试框架?

linux - 使用 TeamViewer 在 Windows xp 上远程安装 Linux

linux - 在 shell 脚本中从 places.sqlite 中的 moz_places 检索数据