C 错误:在公众之后,没有在此范围内声明此范围



试图从此页面修改代码。

这是问题代码:

#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;
}

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

> 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

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

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();
    }
};

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

您在C 之前遇到了相同的问题:

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

这样做的原因是,成员函数的 hody 被对待,就好像在会员可用性方面定义了课堂外。

§9.2 [class.mem] p2

一个类被认为是完全定义的对象类型(3.9)(或完整类型),在 class-specifier 的关闭}处。在类中成员规范化中,该类被视为功能体内的完整 - 非静态数据成员(包括嵌套类中的此类内容)的或平等限制器。否则,它在其自己的类中被认为是不完整的成员规格

因此,只有在类成员体内内部看到的名称(如标准称)可以使用。

我看到了两个可能的修复程序,一个用于您的特定用例,一个用于一般用例。对于您的特定情况,只需使用typename T::const_reverse_iterator即可。对于一般情况,请使用std::declval获取decltype的某种类型的对象,并在此调用该方法:

#include <functional>
decltype(std::declval<T const&>().rbegin()) rbegin() const{ ... }

最新更新