Ошибка компиляции: использование удаленной функции - PullRequest
0 голосов
/ 03 мая 2020

Во время компиляции ошибка: 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

Кредитная карта . 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;
     }
  }

Money заявлено в Money.cpp и Money.h
Деньги. 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;
}

Деньги .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 

1 Ответ

0 голосов
/ 03 мая 2020

Вы должны определить явный конструктор по умолчанию для Node:

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

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