gpt4 book ai didi

c++ - 编译错误: Use of a deleted function

转载 作者:行者123 更新时间:2023-12-02 10:56:40 25 4
gpt4 key购买 nike

在编译期间,错误是:Node::Node() is implicitly deleted because the default definition would be ill-formed。这发生在Node *trans = new Node;中的CreditCard.cpp行上。

CreditCard.h

#ifndef CREDITCARD_H
#define CREDITCARD_H
#include <iostream>
#include "Money.h"

using namespace std;
using String = std::string;

struct Node{
String name;
Money cost;
Node* next;
};

class CreditCard{
private:
String ownerName;
String cardNumber;
Node* transaction;
public:
CreditCard(String, String);

void Charge(String, Money);

void Charge(String, int, int);

void print();

};

#endif

CreditCard.cpp 是:
#include "CreditCard.h"

CreditCard::CreditCard(String name, String cardNo)
:ownerName(name), cardNumber(cardNo){}

void CreditCard::Charge(String name, Money cost){
Node *trans = new Node; // *** This is the line with the error ***
trans->name = name;
trans->cost = cost;
trans->next = transaction;
transaction = trans;
}

void CreditCard::Charge(String name, int euros, int centiments){
Money m(euros, centiments);
Charge(name, m);
}

void CreditCard::print(){
Node *p = transaction;
// p = first;
while(p){
(p->cost.print());
p=p->next;
}
}
MoneyMoney.cppMoney.h中声明
Money.cpp
#include "Money.h"

Money::Money(int euros=0, int centimes=0)
:euros(euros), centimes(centimes)
{}
void Money::setMoney(int euros=0, int centimes=0){
this->euros = euros;
this->centimes = centimes;
}
void Money::print(){
printf("%d,%d Euros\n", this->euros, this->centimes);
}
// Operator Overloading (Operator+)
Money Money::operator+(Money other){
Money total;
total.centimes = this->centimes + other.centimes;
if (total.centimes >= 100){
total.euros = total.euros + (total.centimes / 100);
total.centimes %= 100;
}
total.euros += this->euros + other.euros;
return total;
}

Money.h
#ifndef MONEY_H
#define MONEY_H
#include <iostream>

class Money{
private:
int euros;
int centimes;
public:
Money(int, int);
void setMoney(int, int);
void print();
// Operator Overloading (Operator+)
Money operator+(Money other);
};

#endif

最佳答案

您应该为Node定义一个显式的默认构造函数:

struct Node{
String name;
Money cost;
Node* next;

Node() : name(""), cost(0,0), next(NULL) {} // this should do the trick
};

关于c++ - 编译错误: Use of a deleted function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61568304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com