【问题标题】:Error declaring hash_map in lex file在 lex 文件中声明 hash_map 时出错
【发布时间】:2011-01-19 17:33:55
【问题描述】:

我正在为编译器编写一个简单的预处理器。下面是我的代码的编辑 sn-p:

%{

#include <string.h>
#include <hash_map>
#include "scanner.h"
#include "errors.h"

struct eqstr {
    bool operator()(const char* s1, const char* s2) const {
        return strcmp(s1, s2) == 0;
    }
};

std::hash_map<const char*, char*, hash<const char*>, eqstr> defs; // LINE 28

%}

// Definitions here

%%

// Patterns and actions here

%%

编译时出现以下错误:

dpp.l:28:错误:预期的构造函数, 析构函数,或之前的类型转换 ‘

任何想法可能有什么问题?我几乎从 sgi 文档中复制并粘贴了这一行。

【问题讨论】:

    标签: c++ hashmap lex


    【解决方案1】:

    您需要std::hash 而不仅仅是hash,因为您没有using 语句可以将其拉入范围。此外,默认的std::hash&lt;const char *&gt; 将直接散列指针,这不适用于这种用途——您需要一个散列函数来散列指向的 c 字符串。您需要定义自己的 hash 特化,或者您自己的散列函数——后者可能更好。

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多