自定义C++类,无法重载正确的运算符

问题描述 投票:0回答:1

这是代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <list>

using namespace std;

class Author {

public:

    Author() = default;

    ~Author() {
        delete[] _IDs;
        _IDs = nullptr;
    }

    Author(const char* Name, const char* BirthDate, const int ID)
        : _name(Name)
        , _birthdate(BirthDate)
        , _ID(ID)
    {
        
        for (int i = 0; i < _size; i++) {
            if (ID == _IDs[i]) {
                delete[] _IDs;
                _IDs = nullptr;
                throw new std::invalid_argument("ID Must be UNIQUE;");
            }
        }
            
        check_alloc_ID_size();
        _IDs[_size++] = ID;
    }


    Author(const Author& other)
        : _name(other._name)
        , _birthdate(other._birthdate)
        , _ID(other._ID)
    {}

public:

    const char* GetBD() const { return _birthdate; }

    const char* GetName() const { return _name; }

    const int GetID() const { return _ID; }


private:
    const char* _name;
    const char* _birthdate;
    const int _ID;

    static int* _IDs;
    static int _size;
    static int _alloc_size;

private:

    void check_alloc_ID_size() {
        if (_size == _alloc_size) {
            int* temp = _IDs;
            _alloc_size *= 2;
            _IDs = new int[_alloc_size];

            for (int i = 0; i < _size; i++) {
                _IDs[i] = temp[i];
            }
            delete[] temp;
        }
    }
};

int* Author::_IDs = new int[1];
int Author::_size = 0;
int Author::_alloc_size = 1;

class Book {

public:

    Book(const char* title, const char* isbn, const int year, const Author& author)
        : _title(title)
        , _isbn(isbn)
        , _year(year)
        , _author(author)
    {}

    const char* GetTitle() const { return _title; }
    const char* GetIsbn() const { return _isbn; }
    const int GetYear() const { return _year; }
    const Author& GetAuthor() const { return _author; }

    void PrintBookInfo() {

        printf("%s\n%s\n%d\n", GetTitle(), GetIsbn(), GetYear());
    }

    bool operator==(const Book& other) const {
        return _isbn == other._isbn;
    }


private:
    const char* _title;
    const char* _isbn;
    const int _year;
    const Author& _author;

};

class Patron {
public:

    Patron() {}

    bool operator==(const Patron& other) const {
        return _libID == other._libID;
    }
    
private:

    const char*     _name;
    const char*     _libID;
    vector<Book>    _checked;
};


class Library {

public:

    Library()
    {}

    void AddBook(const Book& book) {
        _books.push_back(make_pair(book, 1));
    }

    
    void RemoveBook(const Book& book) {
        for (auto it = _books.begin(); it != _books.end(); ++it) {
            if ((*it).first == static_cast<const Book&>(book)) {
                _books.erase(it);
                break;
            }
        }
    }
    

    void AddPatron(const Patron& patron) {
        _patrons.push_back(patron);
    }

    
    void RemovePatron(const Patron& patron) {
        for (auto it = _patrons.begin(); it != _patrons.end(); ++it) {
            if (*it == patron) {
                _patrons.erase(it);
                break;
            }
        }
    }
    


private:
    
    vector<pair<Book, int>> _books;
    vector<Patron>          _patrons;

    vector<Book>            _checked_out_books;
    vector<Book>            _available_books;
};


现在错误出现在RemoveBook()函数中,并且与未重载的运算符有关。 我尝试了多种解决方案,甚至聊天 GPT 也无法弄清楚。如果您不确定它是否有效,我可能已经尝试过。非常感谢,非常欢迎!

它说要添加更多详细信息。起初,我在 Book 类中添加运算符重载,然后意识到 _books 如果在库类中。我在这里比较 Pair<> 所以我不确定运算符应该如何进行。就像我说的,我花了 3 个小时使用 chatGPT 给了我错误的答案。

迭代器延续并不像 GPT 所建议的那样重要,你可以直接突破代码,尽管我也尝试过这样做。

operator-overloading
1个回答
0
投票

这是工作代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <list>

using namespace std;

class Author {

public:

    Author() = default;

    ~Author() {
        delete[] _IDs;
        _IDs = nullptr;
    }

    Author(const char* Name, const char* BirthDate, const int ID)
        : _name(Name)
        , _birthdate(BirthDate)
        , _ID(ID)
    {
        
        for (int i = 0; i < _size; i++) {
            if (ID == _IDs[i]) {
                delete[] _IDs;
                _IDs = nullptr;
                throw new std::invalid_argument("ID Must be UNIQUE;");
            }
        }
            
        check_alloc_ID_size();
        _IDs[_size++] = ID;
    }


    Author(const Author& other)
        : _name(other._name)
        , _birthdate(other._birthdate)
        , _ID(other._ID)
    {}

public:

    const char* GetBD() const { return _birthdate; }

    const char* GetName() const { return _name; }

    const int GetID() const { return _ID; }


private:
    const char* _name;
    const char* _birthdate;
    int _ID;

    static int* _IDs;
    static int _size;
    static int _alloc_size;

private:

    void check_alloc_ID_size() {
        if (_size == _alloc_size) {
            int* temp = _IDs;
            _alloc_size *= 2;
            _IDs = new int[_alloc_size];

            for (int i = 0; i < _size; i++) {
                _IDs[i] = temp[i];
            }
            delete[] temp;
        }
    }
};

int* Author::_IDs = new int[1];
int Author::_size = 0;
int Author::_alloc_size = 1;

class Book {

public:
    Book(const char* title, const char* isbn, const int year, const Author& author)
        : _title(title)
        , _isbn(isbn)
        , _year(year)
        , _author(author)
    {}

    const char* GetTitle() const { return _title; }
    const char* GetIsbn() const { return _isbn; }
    const int GetYear() const { return _year; }
    const Author& GetAuthor() const { return _author; }

    void PrintBookInfo() {

        printf("%s\n%s\n%d\n", GetTitle(), GetIsbn(), GetYear());
    }

    bool operator==(const Book& other) const {
        return _isbn == other._isbn;
    }


private:
    const char* _title;
    const char* _isbn;
    int _year = 9;
    Author _author;

};

class Patron {
public:

    Patron() {}

    bool operator==(const Patron& other) const {
        return _libID == other._libID;
    }
    
private:

    const char*     _name;
    const char*     _libID;
    vector<Book>    _checked;
};


class Library {

public:

    Library()
    {}

    void AddBook(const Book& book) {
        _books.push_back(make_pair(book, 1));
    }

    
    void RemoveBook(const Book& book) {
        for (auto it = _books.begin(); it != _books.end(); ++it) {
            if ((*it).first == static_cast<const Book&>(book)) {
                _books.erase(it);
                break;
            }
        }
    }
    

    void AddPatron(const Patron& patron) {
        _patrons.push_back(patron);
    }

    
    void RemovePatron(const Patron& patron) {
        for (auto it = _patrons.begin(); it != _patrons.end(); ++it) {
            if (*it == patron) {
                _patrons.erase(it);
                break;
            }
        }
    }
    


private:
    
    vector<pair<Book, int>> _books;
    vector<Patron>          _patrons;

    vector<Book>            _checked_out_books;
    vector<Book>            _available_books;
};

问题是 _books.erase() 执行 std::move 操作,但是 books 无法提供隐式移动构造函数,因为它有一个 const 引用成员

const Author& _author
需要在构造时初始化。因此,您必须在移动构造函数中提供 Author& ,但您不能这样做,因为它是由 vector.erase() 内部调用的。所以你需要图书类来保存它的副本。

要使此解决方案起作用,您还需要在 Author 类中将

_ID
设置为非 const,否则 Author 无法提供 std::move 所需的空默认构造函数。

© www.soinside.com 2019 - 2024. All rights reserved.