C++错误:未在此范围内声明私有后公开

use*_*322 5 c++ gcc c++11 gcc4.7

试图修改此页面中的代码.

这是问题代码:

#include <iostream>
#include <array>

template<class T>
class const_reverse_wrapper
{
public:
    const_reverse_wrapper (const T& cont)
        : container_(cont)
    {
    }

    decltype( container_.rbegin() ) begin() const
    {
        return container_.rbegin();
    }

    decltype( container_.rend() ) end()
    {
        return container_.rend();
    }

private:
    const T & container_;
};

template<class T>
class reverse_wrapper
{
public:
    reverse_wrapper (T & cont)
        : container_(cont)
    {
    }

    decltype( container_.rbegin() ) begin()
    {
        return container_.rbegin();
    }

    decltype( container_.rend() ) end()
    {
        return container_.rend();
    }
private:
    T & container_;
};

template<class T>
const_reverse_wrapper<T> reversed (const T & cont)
{
    return const_reverse_wrapper<T>(cont);
}

template<class T>
reverse_wrapper<T> reverse (T & cont)
{
    return reverse_wrapper<T>(cont);
}

int main (int argc, char * argv[])
{
    std::array<int,4> a = { 1, 2, 3, 4 };
    for (int i : a)
        std::cout << i;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我得到这些错误:

> g++ -std=c++0x test2.cpp
test2.cpp:13:15: error: 'container_' was not declared in this scope
test2.cpp:13:15: error: 'container_' was not declared in this scope
test2.cpp:18:15: error: 'container_' was not declared in this scope
test2.cpp:18:15: error: 'container_' was not declared in this scope
test2.cpp:36:15: error: 'container_' was not declared in this scope
test2.cpp:36:15: error: 'container_' was not declared in this scope
test2.cpp:41:15: error: 'container_' was not declared in this scope
test2.cpp:41:15: error: 'container_' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

当我在每个类的公共部分之前移动私有部分时,错误就会消失.

template<class T>
class const_reverse_wrapper
{
private:                    // <-----
    const T & container_;   // <-----
public:
    const_reverse_wrapper (const T& cont)
        : container_(cont)
    {
    }

    decltype( container_.rbegin() ) begin() const
    {
        return container_.rbegin();
    }

    decltype( container_.rend() ) end()
    {
        return container_.rend();
    }
};

template<class T>
class reverse_wrapper
{
private:              // <-----
    T & container_;   // <-----
public:
    reverse_wrapper (T & cont)
        : container_(cont)
    {
    }

    decltype( container_.rbegin() ) begin()
    {
        return container_.rbegin();
    }

    decltype( container_.rend() ) end()
    {
        return container_.rend();
    }
};
Run Code Online (Sandbox Code Playgroud)

我尝试使用MinGW GCC 4.6.2和4.7.0进行编译并获得相同的结果.这是一个错误,还是还有其他事情发生?

Xeo*_*Xeo 5

你在C++ 11之前遇到了同样的问题:

struct X{
  Foo f(){ return 42; } // error: 'Foo' does not name a type

  typedef int Foo;
};
Run Code Online (Sandbox Code Playgroud)

这样做的原因是只有成员函数的主体被视为在成员可用性方面被定义为类外.

§9.2 [class.mem] p2

}类说明符结束时,类被视为完全定义的对象类型(3.9)(或完整类型).在类成员规范中,对于非静态数据成员(包括嵌套类中的此类事物),该类在函数体,缺省参数,异常规范大括号或大小写初始化中被视为完整.否则,它在其自己的类成员规范中被视为不完整.

因此,只能使用之前在类成员规范中看到的名称(如标准所称).

我看到了两个可能的修复,一个针对您的特定用例,另一个针对您的特定用例.对于您的具体情况,请使用typename T::const_reverse_iterator.对于一般情况,使用std::declval获取某种类型的对象decltype并调用该方法:

#include <functional>

decltype(std::declval<T const&>().rbegin()) rbegin() const{ ... }
Run Code Online (Sandbox Code Playgroud)