【问题标题】:compiler error expected nested-name specifier编译器错误预期嵌套名称说明符
【发布时间】:2014-01-10 04:11:23
【问题描述】:

我是这个问题的 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)?

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

  • 我找到了相关的标准报价。 7.1.3.2 A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier. In particular, it does not define a new type and it shall not appear in the type-id.。另外,gcc.gnu.org/gcc-4.6/cxx0x_status.html。你会在template alises 旁边看到一个大大的红色否。
  • @remyabel 很好的参考。作为 C++ 新手,我不知道不同的编译器需要不同的语法,或者提供不同的功能集。
【解决方案2】:

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

我假设您已经拥有 gcc 的旧版本,最简单的方法可能是将 PPA 添加到您的存储库并更新和升级您可以毫无顾虑地拥有最新版本:

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

sudo apt-get 更新

这会将新的 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 更新

sudo apt-get upgrade -y

sudo apt-get dist-upgrade

希望这会改变 --version ;)

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 特别是因为我使用的是高于 4.8 但低于 6 的版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多