gpt4 book ai didi

c++ - 在 lex 文件中声明 hash_map 时出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:59:22 24 4
gpt4 key购买 nike

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

%{

#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: error: expected constructor, destructor, or type conversion before ‘<’ token

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

最佳答案

你需要std::hash而不仅仅是 hash , 因为你没有 using将其拉入范围的语句。此外,默认 std::hash<const char *>将直接对指针进行哈希处理,这不适用于此用途——您需要一个哈希函数来对指向的 c 字符串进行哈希处理。您需要定义自己的 hash 特化,或您自己的哈希函数——后者可能更好。

关于c++ - 在 lex 文件中声明 hash_map 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4738598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com