C++ 错误 : was not declared in this scope with private after public

标签 c++ gcc c++11 gcc4.7

试图修改来自 this page 的代码.

问题代码如下:

#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

当我在每个类中将私有(private)部分移动到公共(public)部分之前时,错误就消失了。

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++11 之前遇到了同样的问题:

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

  typedef int Foo;
};

这样做的原因是只有成员函数的主体被视为就成员可用性而言是在类外定义的。

§9.2 [class.mem] p2

A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments, exception-specifications, and brace-or-equal-initializers for non-static data members (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.

因此,只能使用以前在类member-specification(标准调用它)中看到的名称。

我看到了两种可能的修复方法,一种针对您的特定用例,一种针对一般情况。对于您的特定情况,只需使用 typename T::const_reverse_iterator。对于一般情况,使用 std::declvaldecltype 获取特定类型的对象并调用该对象的方法:

#include <functional>

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

关于C++ 错误 : was not declared in this scope with private after public,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13225937/

相关文章:

c# - Windows shell 扩展 : context menu when more than 16 files are selected

C++ 将 2d 位图转换为 3D 使用 C++ 和 OPENGL 库

c++ - gcc 与 visual studio 宏扩展

c++ - 在 C++0x lambda 中通过复制捕获引用变量

可以将整数、 float 、 double 或任何其他可转换为 float 的 C++ 函数

c++ - 为什么回调标志在没有明显原因的情况下被清除和/或损坏?

c++ - 如何使用单个字母组成单词

c - 在不同级别使用不同的枚举类型

c++ - 如何在 C++11 中捕获函数参数并存储函数指针以供以后执行?

c++ - 来自抽象类的 unique_ptr 的 shared_ptr