C++ map 无效转换从int到const LexType&其实是我自己定义的一个int

问题描述

我定义了一个这样的地图:

std::map<std::string,LexType> lexname_s = { { "PROGRAM",PROGRAM}}

还有一个 LexType,就像这样:

typedef enum 
{
    ENDFILE,ERROR,PROGRAM,PROCEDURE,TYPE,VAR,IF,} LexType;

在 Visual Studio Code 中,当我触摸它时总是显示错误类型。

// 我为我所说的添加了更多细节。 线 std::map<std::string,PROGRAM}} 显示错误。似乎我无法以这种方式初始化它。 我在centos7的云服务器中的gcc版本4.8.5 20150623(Red Hat 4.8.5-39)(GCC)中编译它。 并且错误代码显示如下

                 from parse.cpp:1:
../utils.h:52:27: error: invalid conversion from ‘int’ to ‘const LexType&’ [-fpermissive]
           {"ERROR",ERROR}};
                           ^
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,from /usr/include/c++/4.8.2/bits/stl_tree.h:61,from /usr/include/c++/4.8.2/map:60,from ../globals.h:6,from parse.h:4,from parse.cpp:1:
/usr/include/c++/4.8.2/bits/stl_pair.h:112:26: error:   initializing argument 2 of ‘constexpr std::pair<_T1,_T2>::pair(const _T1&,const _T2&) [with _T1 = const std::basic_string<char>; _T2 = LexType]’ [-fpermissive]
       _GLIBCXX_CONSTEXPR pair(const _T1& __a,const _T2& __b)```

解决方法

EOF(在地图定义中显示)是在 stdio.h 中定义的保留宏 这是它的问题。 改个名字就OK了。