C ++重载运算符>> (输入)不会更改原始内容

时间:2015-04-27 11:38:40

标签: c++ string operator-overloading

我正在尝试使用自定义字符串和定义类构建自定义字典类。在尝试重载>>时(输入)我遇到了一些问题。当函数结束时,发送给它的字典不会改变。附上代码: 这是重载:

istream& operator>>(istream& ip, Dictionary& var) {
    Definition temp;
    ip >> temp;
    var += temp;
    return ip;
}

以及其中使用的一些其他功能:

Dictionary& Dictionary::operator+=(Definition& input) {
    if (!checkCopy(input))
    {
        Definition** temp;
        temp = new Definition*[numWords + 1];
        for (int i = 0; i < numWords; i++)
        {
            temp[i] = book[i];
        }
        numWords++;
        temp[numWords - 1] = &input;
        delete[] book;
        book = temp;
    }
    return *this;
}
Dictionary::Dictionary(Dictionary& input) {
    *this = input;
}
Dictionary& Dictionary::operator=(Dictionary& input) {
    if (numWords != 0)
        delete[] book;
    book = new Definition*[input.numWords];
    for (int i = 0; i < input.numWords; i++)
    {
        *this += *input.book[i];
    }
    return *this;
}

班级本身:

class Dictionary
{
private:
    int numWords = 0;
    Definition** book;
public:
    Dictionary();
    ~Dictionary();
    Dictionary(Dictionary&);
    bool operator==(Dictionary&) const;
    Dictionary& operator=(Definition&);
    Dictionary& operator=(Dictionary&);
    friend ostream& operator<<(ostream&, const Dictionary&);
    friend istream& operator>>(istream&, Dictionary&);
    Dictionary& operator-=(int);
    Dictionary& operator+=(Definition&);
    bool checkCopy(Definition&);
    Definition& operator[](int); //left side brackets for input
    Definition operator[](int) const; //right side brackets for output
};

编辑:这里也是定义输入的重载运算符:

istream& operator>>(istream& ip, Definition& var)
{
    cout << "Please enter a word: " << endl;
    ip >> var.word;
    cout << "Please enter the number of definitions for this word: " << endl;
    int idx;
    cin >> idx;
    while (idx<0)
    {
        cout << "Error: number of definitions not possible. Please Try again: " << endl;
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cin >> idx;
    }
    cin.clear();
    cin.ignore(INT_MAX, '\n');
    String* temp = new String[idx];
    for (int i = 0; i < idx; i++) {
        cout << "Please enter the " << i + 1 << "th definition: " << endl;
        cin >> temp[i];
        var += temp[i];
    }
    var.sortDefinition();
    return ip;
}

确实需要帮助。

2 个答案:

答案 0 :(得分:1)

你应该坚持使用std::vector和其他集合类型,而不是像在这里一样使用指针和new / delete

operator+=函数中,您将临时变量的地址复制到数组中:

temp[numWords - 1] = &input;

一旦调用函数operator>>结束,这个指针就不值钱,因为原始对象(Definition temp;)不再存在。因此,该代码的行为是未定义的!

可能通过为Definition定义副本c然后将以上行更改为:

来解决此问题
*temp[numWords - 1] = input;

同样在您的作业运算符中,您正在使用operator+=函数。但是,目前您的numWords成员设置不正确,因此operator+=可能会做错事。因此,在赋值运算符中添加一行,如下所示:

if (numWords != 0)
{
    delete[] book;
    numWords = 0; // add this line
}

答案 1 :(得分:0)

有两个问题:

亚历山大对临时变量所说的话。改为:

Dictionary& Dictionary::operator+=(Definition& input) {
    if (!checkCopy(input))
    {
        Definition** temp;
        temp = new Definition*[numWords + 1];
        temp[0] = new Definition[numWords];
        for (int i = 0; i < numWords; i++)
        {
            temp[i] = book[i];
        }
        temp[0][numWords] = input;
        delete[] book;
        book = temp;
        numWords++;
    }
    return *this;
}

第二个是当我尝试访问由于双指针而未创建的对象中的定义数时,在Definition类中:

Definition** temp;
temp = new Definition*[numWords + 1];

所以我改了它,所以它不能访问它,但首先构建它。

感谢亚历山大的帮助!