c++ - 使用重载的加法运算符有困难

标签 c++ class dynamic operator-keyword addition

这是我的程序。我必须制作一个学生类型的对象,然后让学生“ checkout ”一个项目。我正在使用重载的加法运算符让用户 checkout 该项目。

主要.cpp:

#include <iostream>
#include "Student.h"

using namespace std;

int main() {

    Student s(54000, "JOHN", "DOE");

    cout << "main:" << endl << (s + "Frisbee") << endl << endl;

    system("pause");

    return 0;

}

我在头文件中定义了我所有的类定义,以尽量使这个程序保持最小和简化。

学生.h:

#ifndef STUDENT_H
#define STUDENT_H

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

class Student {

public:

    string firstName;
    string lastName;
    int id;
    int itemsCheckedOut;
    int size;
    string *array;

    Student(int id = 0, string firstName = "", string lastName = "") {
        Student::firstName = firstName;
        Student::lastName = lastName;
        Student::id = id;
        itemsCheckedOut = 0;
        size = 10;
        array = new string[size];
    }

    Student(const Student &other) {
        itemsCheckedOut = other.itemsCheckedOut;
        array = new string[itemsCheckedOut];
        for (int i = 0; i < itemsCheckedOut; i++) {
            array[i] = other.array[i];
        }
    }

    ~Student() {
        delete[] array;
        array = NULL;
    }

    Student &operator=(const Student &rhs) {
        if (this != &rhs) {
            firstName = rhs.firstName;
            lastName = rhs.lastName;
            id = rhs.id;
            itemsCheckedOut = rhs.itemsCheckedOut;
            delete[] array;
            array = new string[size];
            for (int i = 0; i < itemsCheckedOut; i++) {
                array[i] = rhs.array[i];
            }
        }
        return *this;
    }

    void CheckOut(const string &item) {
        array[itemsCheckedOut] = item;
        itemsCheckedOut++;
    }

    friend ostream &operator<<(ostream &output, const Student &student) {
        output << student.id << "  " << student.firstName << " " << student.lastName << endl;
        if (student.itemsCheckedOut != 0) {
            output << student.itemsCheckedOut;
            for (int i = 0; i < student.itemsCheckedOut; i++) {
                output << " " << student.array[i] << endl;
            }
        }
        else {
            output << 0;
        }
        return output;
    }

    const Student operator+(const string &item) {
        Student s;
        s = *this;
        s.CheckOut(item);
        cout << "class:" << endl << s << endl << endl;
        return s;
    }

};

#endif

输出:

class:
54000  JOHN DOE
1 Frisbee

main:
-858993460
1 Frisbee

正如你所看到的,从主体来看,它输出了错误的东西。它不是输出 id 后跟两个空格然后是名字和姓氏,而是输出数字:-858993460。这一定是某种内存泄漏问题或其他问题,但我很确定我的复制构造函数、重载赋值运算符和解构函数都已正确定义,但您可以看一下它们。

我非常感谢任何帮助,因为我在这里变得非常绝望。谢谢。

最佳答案

您的实际 operator+ 看起来是正确的。但是您的复制构造函数和赋值运算符中存在会导致其发生故障的错误:

  • 复制构造函数不设置sizeid 或名称。
  • 复制构造函数应该分配 [size] 项,而不是 [itemsCheckedOut]
  • 赋值运算符不复制size
  • 赋值运算符分配了一个维度为旧大小的新数组,可能会立即导致缓冲区溢出。
  • checkOut 函数不检查它是否写入超过大小。它需要检测这种情况并拒绝结帐或分配更多空间。 (上次您发布有关此项目的问题时我提到了这一点)

关于c++ - 使用重载的加法运算符有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40295804/

相关文章:

c - 在不知道字符串大小的情况下动态提示输入字符串

C++ 类模板(带默认值)

c++ - 如何声明已在静态库中定义的类?

c++ - 是否有任何 C++ XSLT 库?

Java类文件到其他格式的转换,有多少种

delphi - 动态访问 Delphi 组件中的属性

c++ - 如果我将字符串复制到 NULL 指针,为什么 C++ 不打印任何内容?

c++ - 向类中添加新功能

c++ - 我的私有(private)方法如何访问 C++ 中的公共(public)枚举?

iphone - HTML5 iPhone动态缓存图片