ISO C ++ prohibits declaring a tuple without a type

When I try to compile a simple class ( g++ myclass.cpp ), I get the following error:

 ISO C++ forbids declaration of 'tuple' with no type 

I was looking 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 with pair , leaving the second int and including <set> will work.

What am I missing?

EDIT:

Here is the complete conclusion:

 $ 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) 
+6
source share
4 answers

GCC 4.2.1 comes with every mac obsolete. It does not recognize 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 need to link to libC ++, which is clang lib that knows about the features of C ++ 11 instead of the standard libstdC ++ used by gcc.

+12
source

Update! We are at GCC 4.7 these days.

GCC 4.2.1 from all the way back on July 18, 2007 . There is only a remote chance that it supports any functions, starting with C ++ 11.

However, it can provide some of std::tr1 (i.e. std::tr1::tuple<T1, T2, ...> ), in which some of the C ++ 11 functions lived a while before standardization , although from my head they were introduced by GCC only in 4.4.

+5
source

As of gcc 4.2, tuple was in the std::tr1 namespace. You should include <tr1/tuple> and specify your method more or less as follows

 #ifndef MYCLASS #define MYCLASS #include <tr1/tuple> class MyClass { std::tr1::tuple<bool, int, int> my_method(); }; #endif 

Although, as others have said, upgrading to a later gcc might be more appropriate.

+3
source

If you add the -std=c++11 parameter (or for older versions of g++ -std=c++0x ) and add simicolon after the expression in the member function, the code compiles. If this does not work, you may have a version that only defines tuple in the std::tr1 namespace (it seems the implementation contains the <tuple> header, although since there is no error in <tuple> that was not found).

+1
source

All Articles