将新对象插入存储库会引发错误

Inserting new Object to Repository throws error

我已经为它创建了一个对象和一个存储库。 当我尝试将对象插入存储库(使用我创建的插入函数)时,出现编译错误。

我正在尝试插入存储库的 Class

class Payment{
private:
    int day;
    int amount;
    char *type;

public:
    Payment();
    Payment(int day, int amount, char *type);
    Payment(const Payment &p);
    ~Payment();

    //getters
    int getDay()const;
    int getAmount()const;
    char* getType()const;

    //setters
    void setDay(int day);
    void setAmount(int amount);
    void setType(char* type);

    //operator
    Payment& operator=(const Payment& other);
    friend ostream& operator<<(ostream &os,const Payment &obj);
};

//copy constructor
Payment::Payment(const Payment & p){
    this->day = p.day;
    this->amount = p.amount;
    if(this->type!=NULL)
        delete[] this->type;
    this->type = new char[strlen(p.type)+1];
    strcpy_s(this->type, strlen(p.type) + 1, p.type);
}

//assignment operator
Payment& Payment::operator=(const Payment &other) {
    this->day = other.day;
    this->amount = other.amount;
    this->type = new char[strlen(other.type) + 1];
    strcpy_s(this->type, strlen(other.type) + 1, other.type);
    return *this;
}

//destructor
Payment::~Payment(){
    this->day = 0;
    this->amount = 0;
    if (this->type != NULL) {
        delete[]this -> type;
        this->type = NULL;
    }
}


//Repository header
class Repository{
private:
    vector<Payment> list;
public:
    Repository();

    int getLength();

    void insert(const Payment& obj);
    void remove(int position);
};

//Repository cpp
Repository::Repository(){
    this->list.reserve(10);
}

//return the size of the list
int Repository::getLength() {
    return this->list.size();
}

//add payment to list
void Repository::insert(const Payment &obj) {
    this->list.emplace_back(obj);
}

//remove payment from list
void Repository::remove(int position) {
    this->list.erase(this->list.begin() + position);
}

在主要功能中我有

char c[] = "some characters";
Payment pay = Payment(7,9,c);
Repository rep = Repository();
rep.insert(pay);

当我 运行 程序时出现错误“ 表达式:_CrtlsValidHeapPointer(block) "

由于 std::vector 将进行复制,因此 std::vector<Payment> 要求 Payment 具有正确的复制语义。您的复制构造函数和赋值运算符未正确实现。赋值运算符导致内存泄漏,因为您未能 delete [] 现有内存。

最简单的解决方案是放弃使用 char *type; 成员并简单地使用 std::string type;。然后 Payment class 将自动具有正确的复制语义。


鉴于此,对您的 Payment class 的更正如下:

#include <algorithm>
//...
Payment::Payment() : day(0), amount(0), type(nullptr) {}

Payment::Payment(const Payment & p) : day(p.day), amount(p.amount), type(nullptr)
{
    if ( p.type )
    {
        type = new char[strlen(p.type) + 1];
        strcpy_s(this->type, strlen(p.type) + 1, p.type);
    }
}

// Use the copy/swap idiom    
Payment& Payment::operator=(const Payment &other) 
{
    Payment temp(other);  // make a temporary copy
    // swap out contents of temporary with this object
    std::swap(temp.day, day);  
    std::swap(temp.amount, amount);
    std::swap(temp.type, type);
    return *this;  
 }  // when this brace has been reached, the temp copy dies off with the old data

Payment::~Payment()
{
   delete [] type;
}

上面在赋值运算符中使用了copy/swap idiom。复制构造函数使用成员初始化列表。

析构函数不需要检查空指针,因为删除空指针是完全有效的。

现在添加到 std::vector 运行良好,没有任何运行时错误(使用@PaulMcKenzie 发布的代码)。我还找到了一个有效的代码示例,其中只有赋值运算符有点不同。 转换为我的代码将是(并且它正在工作):

Payment& Payment::operator=(const Payment &other) {
    if (this != &other) {
        this->setDay(other.day);
        this->setAmount(other.amount);
        this->setType(other.type);
    }
    return *this;
}

感谢您的帮助!现在它工作完美!我没有从 <algorithm> 图书馆学到很多东西,所以我必须仔细看看。祝你好运! ^_^