不能使用操作符重载比较const和非const模板类型

C++ - Cannot compare const and non-const template types using operator overload

本文关键字:const 和非 类型 比较 操作符 重载 不能      更新时间:2023-10-16

我是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 ElemTypeclass 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

你知道了:这些类是未定义的,编译器甚至不知道它们是否支持==


进一步的问题:

  • ContainerGetElem显然应该在基类中覆盖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 >

这是起点;然后,您可以单独修复所有其他错误。请随意提问。