编译器错误预期嵌套名称说明符

时间:2014-01-10 04:11:23

标签: c++ c++11 compiler-errors gcc4.6

我是问题的OP:Extending a class我收到了很好的答案。但是,当我尝试编译代码(稍微为我的项目重做)时,我收到以下消息(行号改为反映以下示例代码):

except.h: | 09 | expected nested-name-specifier before ‘handler_t1’

还有更多似乎源于这条线的东西。我是C ++的新手,我对答案(以及即将出现的问题)的研究已经产生了这样一个事实:微软的编译器似乎接受了代码,但符合标准的编译器却没有。

我目前拥有的代码如下:

#include <vector>
namespace except
{
  // several other classes and functions which compile and work already
  // (tested and verified) have been snipped out. Entire code is over
  // 1000 lines.

  class Error_Handler
  {
    public:
      using handler_t1 = bool (*)(except::Logic const&);
      std::vector<handler_t1> logic_handlers;

      // a lot more removed because the error has already happened ...
  }
}

通过阅读链接问题中的代码,可以告诉我(我的知识有限)它应该都能正常工作。

因此我的问题是:我需要在此声明/定义中进行哪些更改才能使用gcc进行编译(使用-std = C ++ 0x进行4.6.3 64位linux编译)?

3 个答案:

答案 0 :(得分:7)

GCC 4.6.3不支持C ++ 11类型别名:using handler_t1 = bool (*)(except::Logic const&);。非模板类型别名等同于typedef:typedef bool (*handler_t1)(except::Logic const&);。替换它们,看看是否有帮助。

甚至更好,升级到更新的编译器版本。我相信这里的常规响应者倾向于写入GCC 4.8编译的语言部分。

编辑:我在答案中看到的唯一其他iffy功能是基于范围的for,我相信GCC在4.6中增加了支持。用typedef替换类型别名后,你应该没问题。

答案 1 :(得分:2)

使用版本6之前的g ++,您需要选项--std=c++11才能使用using指令。

答案 2 :(得分:1)

我也遇到了同样的问题,只需在我的Ubuntu中升级go G ++ 4.8即可解决。

我假设您已经拥有以前版本的gcc,最简单的方法是将PPA添加到您的存储库并更新和升级您可以使用最新版本而不用担心:

  

sudo add-apt-repository ppa:ubuntu-toolchain -r / test

     

sudo apt-get update

这会将新的PPA添加到其他来源。

然后取消替代方案:

  

sudo update-alternatives --remove-all gcc

     

sudo update-alternatives --remove-all g ++

然后:

  

sudo apt-get install gcc-4.8

     

sudo apt-get install g ++ - 4.8

并作为替代包安装:

  

sudo update-alternatives --install / usr / bin / gcc gcc /usr/bin/gcc-4.8 20

     

sudo update-alternatives --install / usr / bin / g ++ g ++ /usr/bin/g++-4.8 20

     

sudo update-alternatives --config gcc

     

sudo update-alternatives --config g ++

最后:

  

sudo apt-get update

     

sudo apt-get upgrade -y

     

sudo apt-get dist-upgrade

希望这会改变--version;)