实现类似字符串的类时"no type named 'const_reference' "错误
"no type named 'const_reference' " error when implementing a string-like class
我正在尝试实现与std :: string相似行为的类,并且我在std ::复制行中收到错误:
Str& operator+=(const Str& s){
std::copy(s.data.begin(), s.data.end(), std::back_inserter(data));
return *this;
}
'数据'是Vec&lt类型的对象;char>,而vec是我自己实施的类似矢量的类别,似乎自己工作正常。
它也说:
c: mingw bin .. lib gcc gcc mingw32 3.4.2 ............................................ include c 3.4.2 bits stl_iterator.h ||:: back_insert_iterator<Vec<char>>':|
听起来您的 vec
不符合容器要求,因此不能保证与容器一起使用的标准设施(例如back_inserter
)可用。
C 11中的表96中指定了要求,尽管C 98中的表65可能更适合您的古代编译器。这些要求之一是嵌套的const_reference
类型。
检查std::back_inserter
和std::copy
的要求。尤其是,std::back_inserter
期望一个可以满足概念容器的参数。至少这意味着实施标准的§23.2.1,其中列出的一个要求之一是:
X::const_reference
|T
的const lvalue |编译时间
即。容器类型中的typedef const_reference
。
back_inserter
是一种说服力函数,可在容器上构造back_insert_iterator
;在这种情况下,data
。
data
,您已经说过是您自己的本地 vector
-type类。为了使此工作起作用,您的vector
类必须具有const_reference
Typedef定义。这样的东西:
template <typename Item>
class Vec
{
public:
typedef const Item& const_reference;
};
任何实现容器的其他要求。这些在C 03标准中概述,在 23.1中的容器需求中,包括表65。
另请参阅此问题以讨论要求。
尝试添加
typedef t value_type;
typedef const value_type&amp;const_reference;
在您的vec&lt; t>身体中。
问题需要更多细节,例如您的VEC类。
您遇到的错误到底是什么?请分享有关错误的更多信息。控制台日志会有所帮助。
std ::复制带有两个输入迭代器。(http://www.cplusplus.com/reference/algorithm/copy/)您确定像班级一样的向量是否正确处理迭代器?
另外,请检查您的VEC是否支持Back_inserter所需的容器要求。http://www.cplusplus.com/reference/eriterator/back_inserter/
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 与映射和unordered_map相关的编译错误:"attempting to reference a deleted function"
- 索引 2D 向量时'Reference to type const..'编译错误
- 为什么我收到以下错误:"returning reference to temporary [-Werror=return-local-addr]"
- 包含<iostream>时"Undefined reference"错误
- CMake 'undefined reference' CERN-ROOT 的错误
- 多级继承错误 C2280:"atttempting to reference a deleted function"
- 生成错误:"cannot find target for file"和"undefined reference"
- YAML-cpp 编译错误:"undefined reference to YAML::LoadFile"
- 添加 cpp11 插件时出现错误消息"Undefined reference to boost (...)"
- 使用程序集嵌入数据时"Undefined reference"错误,使用适用于窗口的 mingw-w64 编译(COFF 而不是 ELF)
- 如何修复 cpp 中的"undefined reference to"错误,当调用刚性车身树中的函数时,如 doKinematics、CreateKinematicCache
- 带有 mingw-w64 的 SDL2 - 链接器找到 libSDL2.a,但抛出"undefined reference"错误
- 如何解决棉绒错误"Reference initialization causes loss of const/volatile integrity"
- 我应该如何纠正错误"reference to 'p' is ambiguous"?
- 在优先级队列上调用推送时出现错误"reference to non-static member function must be called"