Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运算符重载[]和=_C++_Operator Overloading - Fatal编程技术网

C++ 运算符重载[]和=

C++ 运算符重载[]和=,c++,operator-overloading,C++,Operator Overloading,我试图构建一个模板类,它是C#dictionary类的简化版本,称为“Map”。我遇到的问题是[]运算符重载和=运算符重载 []运算符重载应以允许mapObject[“keyString”]=value等语句的方式重载;在这种情况下,如果密钥不存在,则必须抛出异常。 重载=运算符应执行深度复制 我做错了什么 #pragma once #include <iostream> #include <ostream> using namespace std; temp

我试图构建一个模板类,它是C#dictionary类的简化版本,称为“Map”。我遇到的问题是[]运算符重载和=运算符重载

[]运算符重载应以允许mapObject[“keyString”]=value等语句的方式重载;在这种情况下,如果密钥不存在,则必须抛出异常。 重载=运算符应执行深度复制

我做错了什么

    #pragma once
#include <iostream>
#include <ostream>

using namespace std;

template<class K, class V>
class Map;

template <class K, class V>
class Entry;

//template<class K, class V>
//Map<K, V>& operator[](K* sKey);

template<class K, class V>
ostream& operator<< <>(ostream& os, const Map<K, V>& L);

template <class K, class V>
class Map
{
protected:
    Entry<K, V>     **ledger;
    Entry<K, V>     **temp;
    int             numOfEntries;

public:
    V               *valArray;

    Map();
    Map(const Map<K, V>& mapObject);
    ~Map();

    void ledgerAllocation();
    void tempAllocation();
    void entrySizePlus();
    void ledgerCopy();
    void tempCopy();
    void AddEntry(K key, V value);

    Map<K, V>& operator[](K* sKey);
    friend ostream& operator<< <>(ostream& os, const Map& L);
    Map<K, V>& operator=(const Map<K, V>& toBeCopied);
};


//Default constructor, might change at a later stage...
template<class K, class V>
Map<K, V>::Map()
{
    numOfEntries = 1;
}

template<class K, class V>
inline Map<K, V>::Map(const Map<K, V>& mapObject)
{
    ledger = new Entry<K, V>*;
    numOfEntries = 1;
    ledger = mapObject.ledger
}

//Default destructor, will need to change it later to properly delete an allocated array.
template<class K, class V>
Map<K, V>::~Map()
{
}

template<class K, class V>
inline void Map<K, V>::ledgerAllocation()
{
    ledger = new Entry<K, V>*[numOfEntries];

    for (int i = 0; i < numOfEntries; i++)
    {
        ledger[i] = new Entry<K, V>;
    }
}

template<class K, class V>
inline void Map<K, V>::tempAllocation()
{
    temp = new Entry<K, V>*[numOfEntries];

    for (int i = 0; i < numOfEntries; i++)
    {
        temp[i] = new Entry<K, V>;
    }
}

template<class K, class V>
inline void Map<K, V>::entrySizePlus()
{
    tempAllocation();
    ledgerCopy();

    ledgerAllocation();
    tempCopy();
}

template<class K, class V>
inline void Map<K, V>::ledgerCopy()
{
    tempAllocation();
    K cKey;
    V cValue;

    for (int i = 0; i < numOfEntries - 1; i++)
    {
        cKey = ledger[i]->getKey();
        cValue = ledger[i]->getValue();
        temp[i]->setKeyandValue(cKey, cValue);
    }
}

template<class K, class V>
inline void Map<K, V>::tempCopy()
{
    ledgerAllocation();
    K cKey;
    V cValue;

    for (int i = 0; i < numOfEntries - 1; i++)
    {
        cKey = temp[i]->getKey();
        cValue = temp[i]->getValue();
        ledger[i]->setKeyandValue(cKey, cValue);
    }
}

//Function that will add a new Key and Value to a new map-object within the array.
template<class K, class V>
void Map<K, V>::AddEntry(K newKey, V newValue)
{

    if (numOfEntries <= 1)
    {
        ledgerAllocation();
    }
    else
    {
        entrySizePlus();
    }

    this->ledger[numOfEntries - 1]->setKeyandValue(newKey, newValue);
    numOfEntries++;
}


template<class K, class V>
Map<K, V>& Map<K, V>::operator[](K * sKey)
{
    K Akey;
    V Avalue;

    for (int i = 0; i < numOfEntries; i++)
    {
        Akey = ledger[i]->getKey();
        Avalue = ledger[i]->getValue();

        if (Akey == *sKey)
        {
            return Avalue;
        }
        else
        {
            throw string("Key does not exist");
        }
    }
}

template<class K, class V>
Map<K, V>& Map<K, V>::operator=(const Map<K, V>& toBeCopied)
{
    if (ledger == toBeCopied)
    {
        return *this;
    }
    ledger = toBeCopied.ledger;

    return *this;
}

//Overloads the "<<" so that the command: cout << mapObject << endl; can be used to print the entire map.
template<class K, class V>
ostream & operator<< <>(ostream& os, const Map<K, V>& L)
{
    K pKey;
    V pValue;

    for (int i = 0; i < L.numOfEntries - 1; i++)
    {
        pKey = L.ledger[i]->getKey();
        pValue = L.ledger[i]->getValue();
        os << "[" << pKey << " , " << pValue << "]";
        if (i < L.numOfEntries - 2)
        {
            cout << " , ";
        }
    }
    return os;
}


template <class K, class V>
class Entry
{
protected:
    K key;
    V value;

public:
    Entry();
    ~Entry();

    void setKeyandValue(K skey, V sValue);
    K getKey();
    V getValue();
    void printKeyandValue();

};


template<class K, class V>
Entry<K, V>::Entry()
{
    this->key = NULL;
    this->value = NULL;
}

template<class K, class V>
Entry<K, V>::~Entry()
{
}

template<class K, class V>
inline void Entry<K, V>::setKeyandValue(K skey, V sValue)
{
    key = skey;
    value = sValue;
}

template<class K, class V>
inline K Entry<K, V>::getKey()
{
    return this->key;
}

template<class K, class V>
inline V Entry<K, V>::getValue()
{
    return this->value;
}

template<class K, class V>
void Entry<K, V>::printKeyandValue()
{
    cout << this->key << ": " << this->value << endl;
}
#pragma一次
#包括
#包括
使用名称空间std;
模板
类图;
模板
班级报名;
//模板
//地图和操作员[](K*sKey);
模板
ostream&operatorgetValue();
温度[i]->setKeyandValue(cKey,cValue);
}
}
模板
内联void映射::tempCopy()
{
定位();
克奇;
V值;
对于(int i=0;igetKey();
cValue=temp[i]->getValue();
分类账[i]->setKeyandValue(cKey,cValue);
}
}
//函数,该函数将向数组中的新映射对象添加新的键和值。
模板
void Map::AddEntry(K newKey,V newValue)
{
if(numOfEntries分类账[numOfEntries-1]->setKeyandValue(newKey,newValue);
numOfEntries++;
}
模板
映射和映射::运算符[](K*sKey)
{
K阿克伊;
V阿瓦卢;
对于(int i=0;igetKey();
Avalue=ledger[i]->getValue();
如果(Akey==*sKey)
{
返回值;
}
其他的
{
抛出字符串(“键不存在”);
}
}
}
模板
映射和映射::运算符=(常量映射和toBeCopied)
{
如果(分类账==待分类账)
{
归还*这个;
}
分类账=待分类账。分类账;
归还*这个;
}
//重载“
这里的错误是,当此函数返回时,返回的对象引用超出了范围。一旦此函数返回,此引用就不再有效

    Avalue = ledger[i]->getValue();
在这里,您正在调用
getValue()
,并将此方法调用的结果放入一个对象中,该对象是在该函数中声明的“本地”对象

    return Avalue;
这将返回对该对象的引用。但由于这是该函数的本地对象,因此一旦返回该对象,该对象就不再存在

您的一般方法是正确的,您的
操作符[]
需要返回一个引用

但是,您需要返回对容器中原始值的引用,而不是“本地”对象,它是在
操作符[]
方法中创建的临时对象,当此方法返回时将被销毁

重载=运算符应执行深度复制


Map&Map::operator=(const-Map&toBeCopied)中
行:
ledger=toBeCopied.ledger;
不执行深度复制。两个对象将指向同一个分类账。

您知道
std::map
,是吗?是的,ofc,但我正在尝试自己的版本。好的,当然。然后我建议将代码归结为实际问题。这可能对您有用/感兴趣:。因此我仍然使用Akey=ledger[i]->getKey();来比较键?比较键与正确返回值无关。因此,我可以删除V Avalue;Avalue=ledger[i]->getValue();返回Avalue;并将其替换为类似于return-ledger[i]。value;?当我测试运算符重载时,我得到一个错误“no operator”[]匹配这些操作数。和C2679“binary”[”:未找到接受类型为“int”的右操作数的运算符(或没有可接受的转换)。这是为什么?当然,因为它有问题。您应该修改此问题,或发布新的。
    return Avalue;