ISO C++ forbids declaration of ‘tuple’ with no type
Solution 1
GCC 4.2.1 shipped with every mac is outdated. It will not recognize the C++11.
You need to compile your code using: c++ instead of g++ which calls clang, which is the officially updated compiler on mac.
c++ -std=c++11 -stdlib=libc++ myclass.cpp -o prog
You are required to link against libc++ which is clang lib which knows about c++11 features instead of the default libstdc++ used by gcc.
Solution 2
Update! We're on GCC 4.7 these days.
GCC 4.2.1 is from all the way back on 18th July, 2007. There is only a remote chance that it supports any features from what became C++11.
That said, it may provide some in std::tr1
(i.e. std::tr1::tuple<T1, T2, ...>
), which is where some of the C++11 features lived in the time before standardisation, though off the top of my head these were introduced to GCC only in 4.4.
Solution 3
With gcc 4.2, tuple
was in namespace std::tr1
. You must include <tr1/tuple>
and specify your method more or less like this
#ifndef MYCLASS
#define MYCLASS
#include <tr1/tuple>
class MyClass {
std::tr1::tuple<bool, int, int> my_method();
};
#endif
Although, as others already suggested, updating to a more recent gcc might be more appropriate.
Solution 4
If you add the -std=c++11
(or, for older versions of g++
the -std=c++0x
) option and add a simicolon after the expression in the member function the code compiles. If this doesn't work you might have a version which only defines tuple
in namespace std::tr1
(it seems, the implementation provides a <tuple>
header, though, because there is no error about <tuple>
not being found).
Jawap
Updated on June 06, 2022Comments
-
Jawap almost 2 years
When trying to compile a simple class (
g++ myclass.cpp
), I get the following error:ISO C++ forbids declaration of ‘tuple’ with no type
I searched for this problem, and in most cases people seemed to forget
std::
or including<tuple>
in the header. But I have both. Here is my code:myclass.h
#ifndef MYCLASS #define MYCLASS #include <iostream> #include <tuple> class MyClass { std::tuple<bool, int, int> my_method(); }; #endif
myclass.cpp
#include "myclass.h" using namespace std; tuple<bool, int, int> MyClass::my_method() { return make_tuple(true, 1, 1); }
If I do the same using
pair
instead, leaving out the secondint
and including<set>
, it works.What am I missing?
EDIT:
Here is the full output:
$ g++ myclass.cpp -o prog
In file included from myclass.cpp:1:
myclass.h:7: error: ISO C++ forbids declaration of ‘tuple’ with no type
myclass.h:7: error: invalid use of ‘::’
myclass.h:7: error: expected ‘;’ before ‘<’ token
myclass.cpp:5: error: expected constructor, destructor, or type conversion before ‘<’ token$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658)
(LLVM build 2336.11.00)-
Dietmar Kühl over 11 yearsDid you specify
-std=c++11
when compiling the code? -
Zaur Nasibov over 11 years
using namespace std;
- What a blasphemy! -
Zeta over 11 yearsCan't reproduce this error. Are you sure that you used exactly this code? You should mark the exact line g++ reported.
-
Jawap over 11 yearsMy compiler doesn't seem to support C++11:
$ g++ -std=c++11 myclass.cpp -o prog cc1plus: error: unrecognized command line option "-std=c++11"
. I'm usingi686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1
. Would you recommend trying to change compiler or is there an alternative totuple
for older compilers? -
Puppy over 11 yearsUh, I don't see any
#include <tuple>
in your header. -
Basile Starynkevitch about 5 yearsConsider upgrading GCC, perhaps by downloading its source code (
gcc-8.3
) and compiling that.
-
-
Jawap over 11 yearsIs there any reason, why on my 2012 Mac (OS X 10.8.2, latest Xcode) I would have such an old GCC? (I didn't install it myself, Xcode probably did). I'm afraid of breaking my environment if I try to update.
-
Jawap over 11 yearsIs there any reason, why on my 2012 Mac (OS X 10.8.2, latest Xcode) I would have such an old GCC? (I didn't install it myself, Xcode probably did). I'm afraid of breaking my environment if I try to update.
-
Lightness Races in Orbit over 11 years@Jawap: Beats me. Ask the packaging team.
-
Kirell over 11 yearsNo it is normal. Apple has moved to clang and dropped gcc but they keep it not to break retro compatibility.
-
Kirell over 11 yearsIt is likely that you will break your environnement if you try to update gcc. Use clang, it is faster anyway ...
-
Kirell over 11 yearsIt is not a good idea to update gcc on mac. It will break compatibility. You can have a gcc but not in /usr/bin/gcc
-
Steve Jessop over 11 years@Kikhos: out of interest, compatibility of what? What kind of thing is it in OSX that depends on /usr/bin/gcc being 4.2.1?