我是问题的 OP:Extending a class我收到了一个很好的答案。然而,当我尝试编译代码(为我的项目稍微修改)时,我收到了以下消息(行号已更改以反射(reflect)以下示例代码):
except.h: | 09 | expected nested-name-specifier before ‘handler_t1’
以及更多似乎源于这一行的内容。我是 C++ 的新手,我对答案(以及即将出现的问题)的研究得出了这样一个事实:Microsoft 的编译器似乎接受了代码,但符合标准的编译器却不接受。
我目前的代码如下:
#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 进行编译(4.6.3 64 位 linux 使用 -std=C++0x 进行编译)?
最佳答案
GCC 4.6.3 不支持 C++11 类型别名:using handler_t1 = bool (*)(except::Logic const&);
。非模板类型别名等同于 typedef:typedef bool (*handler_t1)(except::Logic const&);
。替换它们,看看是否有帮助。
或者更好的是,升级到更新的编译器版本。我相信这里的常规响应者倾向于写入由 GCC 4.8 编译的语言部分。
编辑:我在该答案中看到的唯一其他不确定的功能是基于范围的功能,我相信 GCC 在 4.6 中增加了对它的支持。用 typedef 替换类型别名后你应该没问题。
关于c++ - 编译器错误预期嵌套名称说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21036062/