简体   繁体   中英

ISO C++ forbids declaration of ‘tuple’ with no type

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 second int 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)

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.

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 (ie 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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM