试图修改来自 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::declval
为 decltype
获取特定类型的对象并调用该对象的方法:
#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/