可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am the OP for the question: Extending a class in 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):
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.
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)?
回答1:
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.
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.
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.
回答2:
I also got the same problem and it solved by just upgrading go G++ 4.8 in my Ubuntu.
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:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
this will add the new PPA to the other sources.
Then unistall the alternative:
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
then:
sudo apt-get install gcc-4.8
sudo apt-get install 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++
at the end:
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade
Hope this changes the --version ;)
回答3:
With g++ before version 6 you need the option --std=c++11
to be able to use the using
directive.