c++ - 位置.hh :46: error: expected unqualified-id before ‘namespace’

标签 c++ bison yacc flex-lexer

这是我的代码:

 34  
 35 /**  
 36  ** \file position.hh  
 37  ** Define the example::position class.  
 38  */  
 39   
 40 #ifndef BISON_POSITION_HH   
 41 #define BISON_POSITION_HH   
 42    
 43 #include <iostream>   
 44 #include <string>   
 45    
 46 namespace example   
 47 {   
 48   /// Abstract a position.   
 49   class position   
 50   {   
 51   public:   
 52    
 53     /// Construct a position.   
 54     position ()    
 55       : filename (0), line (1), column (0)   
 56     {    

谢谢,speeder,太好了。 Necrolis,也谢谢你。你们两个在编译单元上都在同一条轨道上。这是完整的错误报告:

在 location.hh:45 包含的文件中, 来自 parser.h:64, 来自 scanner.h:25, 来自 scanner.ll:8: position.hh:46: error: ‘namespace’ 之前应为 unqualified-id

location.hh 看起来像这样:

35 /**
36  ** \file location.hh
37  ** Define the example::location class.
38  */
39 
40 #ifndef BISON_LOCATION_HH
41 # define BISON_LOCATION_HH
42 
43 # include <iostream>
44 # include <string>
45 # include "position.hh"
46 
47 namespace example
48 {
49 
50   /// Abstract a location.
51   class location
52   {
53   public:

我还应该补充一点,这些文件是由 bison 生成的。当我尝试编译由 flex++ 生成的 c++ 扫描器类时,我进入了这个阶段。我通过发出 flex --c++ -o scanner.cc scanner.ll 获取 .cc 代码。

最佳答案

这发生在 ;或者在命名空间之前缺少其他一些关闭的东西。你确定34之前的那几行没有代码吗?如果他们有代码(即使该代码是其他#include),错误就在那里。

编辑:或者如果所有 34 行都没有代码,则错误出在包含此 header 的文件上,很可能有一个没有结尾的代码;或 } 或 ) 或其他一些结束字符,紧随其后(当然忽略注释)是#include position.hh

或者如果一行中有两个include,一个在position.hh之前,position.hh之前包含的header的最后一行是有错误的,通常是没有;的结构结束后

关于c++ - 位置.hh :46: error: expected unqualified-id before ‘namespace’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2234294/

相关文章:

c++ - 使用按位运算符解码 Int (C++)

c++ - 如何简化 C++ bool 比较

c - Bison 多个具有属性的非终结符

bison - 如何解决减少减少冲突 :

c++ - C++ lambda或基于lambda基于签名的重载解析中的可选/忽略参数

c++ - 在多维 vector 中搜索两个特定元素

在 Bison 中找不到 'syntax error' 消息的原因

使用 Flex 和 Bison 创建解析器的输入函数

戈雅克 : getting context to the yacc parser/no `%param`

c - 解析多个文件时错误的 lex 状态