不能使用操作符重载比较const和非const模板类型
C++ - Cannot compare const and non-const template types using operator overload
我是Stack Overflow和c++的新手!那么问题来了:
目标是使用next接口创建容器类:IContainer.h:
class ElemNotFound {};
template < class ElemType, class IndexType > class IContainer
{
public:
virtual const ElemType& GetElem(const IndexType& index) const throw (ElemNotFound) = 0;
virtual void PutElem(const IndexType& index, const ElemType& elem) throw () = 0;
};
当前使用该接口的代码是:
#include "IContainer.h"
#include <vector>
class Container : public IContainer < class ElemType, class IndexType >
{
private:
struct ContainerElement
{
IndexType& Index;
ElemType& Data;
};
std::vector < ContainerElement > MyVector;
std::vector < ContainerElement > ::iterator MyIterator;
public:
// EDIT: that operator== part is incorrect as
// failed attempt to circumvent inability to compare custom types
friend bool operator== (IndexType& x, const IndexType& y)
{
if (x == y) return 1;
else return 0;
}
const ElemType& GetElem(const IndexType& index)
{
try
{
MyIterator = MyVector.begin();
while (MyIterator != MyVector.end())
{
if (MyIterator->Index == index)
// PROBLEM: missing operator "==" for IndexType == const IndexType
{
// do useful things
}
MyIterator++;
}
}
catch (Exception e) // everything down below is a placeholder
{
throw (ElemNotFound) = 0;
}
}
void PutElem(const IndexType& index, const ElemType& elem)
{
}
};
IndexType和const IndexType(使用"==")的直接比较不工作,因为我不知道的原因。我想比较向量中的自定义索引和函数中用于从容器中取出元素的索引。对自定义类型使用操作符重载"=="也不起作用。应该是不正确的继承还是操作符重载的不正确使用——我不知道!
所以问题是:如何比较使用模板的类中的const和非const自定义类型变量?你的代码有一个根本的问题;你看到的所有其他错误都只是隐藏了真正的错误。就是这一行:
class Container : public IContainer < class ElemType, class IndexType >
class ElemType
和class IndexType
参数具有误导性。这些实际上是从未定义的类的前向声明。它们的名称与IContainer
的模板参数名称相同只是巧合。
换句话说:你正在用不完整的类实例化你的模板。
这意味着编译器对它们几乎一无所知。他们有公共建造者吗?他们支持operator==
吗?
考虑这个极其简化的程序版本:
template < class ElemType, class IndexType > class IContainer
{
public:
virtual void PutElem(const IndexType& index, const ElemType& elem);
};
class Container : public IContainer < class ElemType, class IndexType >
{
public:
void PutElem(const IndexType& index, const ElemType& elem)
{
bool b1 = index == index;
bool b2 = elem == elem;
}
};
int main()
{
Container c;
}
编译错误(取决于你的编译器):
stackoverflow.cpp(12) : error C2676: binary '==' : 'const IndexType' does not define this operator or a conversion to a type acceptable to the predefined operator
stackoverflow.cpp(13) : error C2676: binary '==' : 'const ElemType' does not define this operator or a conversion to a type acceptable to the predefined operator
你知道了:这些类是未定义的,编译器甚至不知道它们是否支持==
。
进一步的问题:
-
Container
的GetElem
显然应该在基类中覆盖GetElem
,但它是const
。覆盖区分const
和非const
。 -
MyIterator = MyVector.begin();
行不能在const
函数中工作,因为MyIterator
被修改了(而不是mutable
)。 - 你的模板不能处理像
int
这样的基本类型,因为如果两个操作数都是基本类型,你不能重载operator==
。
我不完全确定你的代码的意图是什么,但也许你想要Container
是一个模板,以及,用于生成Container
类?
你可以这样做:
template < class ElemType, class IndexType >
class Container : public IContainer < ElemType, IndexType >
这是起点;然后,您可以单独修复所有其他错误。请随意提问。
相关文章:
- 如何避免将 const 和非 const 成员函数输入到模板中的代码重复
- 如何避免重复的const和非const虚拟函数?是否可以
- 避免使用 auto 关键字从字面上复制 const 和非 const 的代码?
- 为什么在const和非const方法中删除代码重复不是不确定的行为
- 返回一个 const 和非 const 包装器对象
- 在使用指向 const 和非 const 方法的成员指针时减少模板专用化的数量
- 为什么BOOST.RANGE RANGE_BEGIN/END FREE功能对const和非const引用都重载
- Const和非常量函子
- 如何删除抽象类中类似的 const 和非 const 成员函数之间的代码重复
- 同一函数的const和非const版本——反模式
- 避免在const和非const成员函数中重复代码
- c++中返回引用的重载const和非const类方法
- 公开begin()和end()的const和非const版本,以使用智能指针迭代成员向量
- 树的const和非const版本的访问模式
- 应用于const和非const对象的引用返回方法
- 如何专门化const和非const容器的模板
- 定义遍历函数的const和非const版本
- 宏解决方案的重复,const和非const, getter
- Range-v3:使用view_facade来同时提供const和非const迭代器
- 不能使用操作符重载比较const和非const模板类型