C 错误:在公众之后,没有在此范围内声明此范围
C++ error: was not declared in this scope with private after public
试图从此页面修改代码。
这是问题代码:
#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{ ... }
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 错误:未在此范围内声明'reverse'
- 并行用于C++17中数组索引范围内的循环
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 不计算一个范围内的完美数
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 在函数内创建的对象的范围 - 如果在函数外部存储和访问引用,它们是否有效?
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 未在此范围内声明错误 'xy'
- set_intersection范围范围
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 命名空间范围内的外部 - GCC vs clang vs msvc
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- 满足标准::范围::范围概念
- 向量向量的范围范围
- 用辅助方法范围范围的问题来操纵字符串
- 用于循环范围范围
- 矢量超出范围/范围检查