C++ 编译器错误预期嵌套名称说明符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21036062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compiler error expected nested-name specifier
提问by Jase
I am the OP for the question: Extending a classin which I received an excellent answer. However, as I try to compile the code (reworked slightly for my project) I received the following message (line no. changed to reflect following sample code):
我是这个问题的 OP:扩展一个我得到了很好答案的课程。但是,当我尝试编译代码(为我的项目稍作修改)时,我收到了以下消息(行号已更改以反映以下示例代码):
except.h: | 09 | expected nested-name-specifier before ‘handler_t1'
along with many more which seem to stem from this line. I am brand new to C++, and my research into the answer (and the forthcoming problem) has yielded this fact: Microsoft's compiler seems to accept the code, but standards compliant ones do not.
以及更多似乎源于这条线的东西。我是 C++ 的新手,我对答案(以及即将出现的问题)的研究得出了这样一个事实:Microsoft 的编译器似乎接受代码,但符合标准的编译器不接受。
The code as I currently have it is as follows:
我目前拥有的代码如下:
#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 ...
}
}
A read through of the code in the linked question indicates to me (with my limited knowledge) that it should all work.
通读链接问题中的代码向我表明(以我有限的知识)它应该都可以工作。
My question therefore is: What do I need to change in this declaration/definition to enable this to compile with gcc (4.6.3 64 bit linux compiling with -std=C++0x)?
因此,我的问题是:我需要在此声明/定义中更改什么才能使其能够使用 gcc 进行编译(4.6.3 64 位 linux 使用 -std=C++0x 进行编译)?
回答by Casey
GCC 4.6.3 doesn't support C++11 type aliases: using handler_t1 = bool (*)(except::Logic const&);
. Non-template type aliases are equivalent to typedefs: typedef bool (*handler_t1)(except::Logic const&);
. Replace them and see if that helps.
GCC 4.6.3 不支持 C++11 类型别名:using handler_t1 = bool (*)(except::Logic const&);
. 非模板类型别名等价于 typedefs: typedef bool (*handler_t1)(except::Logic const&);
。更换它们,看看是否有帮助。
Or even better, upgrade to a more recent compiler version. I believe the regular responders here tend to write to the portion of the language compiled by GCC 4.8.
或者甚至更好,升级到更新的编译器版本。我相信这里的常规响应者倾向于写 GCC 4.8 编译的语言部分。
EDIT: The only other iffy feature I see in that answer is range-based-for, which I believe GCC added support for in 4.6. You should be OK after replacing the type aliases with typedefs.
编辑:我在该答案中看到的唯一其他不确定的功能是基于范围的,我相信 GCC 在 4.6 中增加了对它的支持。用 typedef 替换类型别名后,您应该没问题。
回答by rwst
With g++ before version 6 you need the option --std=c++11
to be able to use the using
directive.
对于版本 6 之前的 g++,您需要--std=c++11
能够使用该using
指令的选项。
回答by Ali786
I also got the same problem and it solved by just upgrading go G++ 4.8 in my Ubuntu.
我也遇到了同样的问题,只需在我的 Ubuntu 中升级 go G++ 4.8 就解决了。
I assume that you already have a former version of gcc, the easiest way could be adding PPA to your repositories and Update and upgrade you can have the latest version with no worries:
我假设您已经拥有以前版本的 gcc,最简单的方法是将 PPA 添加到您的存储库中,然后更新和升级您可以毫无后顾之忧地拥有最新版本:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get 更新
this will add the new PPA to the other sources.
这会将新的 PPA 添加到其他来源。
Then unistall the alternative:
然后卸载替代方案:
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
须藤更新替代品 --remove-all gcc
须藤更新替代品 --remove-all g++
then:
然后:
sudo apt-get install gcc-4.8
sudo apt-get install g++-4.8
须藤 apt-get 安装 gcc-4.8
须藤 apt-get 安装 g++-4.8
and as the alternative packages install :
并作为替代软件包安装:
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++
须藤更新替代品 --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20
须藤更新替代品 --install /usr/bin/g++ g++ /usr/bin/g++-4.8 20
须藤更新替代品 --config gcc
须藤更新替代品 --config g++
at the end:
在末尾:
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade
sudo apt-get 更新
sudo apt-get upgrade -y
sudo apt-get dist-upgrade
Hope this changes the --version ;)
希望这会改变 --version ;)