错误:不完整类型“类”的无效使用

问题描述

我在尝试使用 Credit 对象(派生)创建 Payment 对象(base)时遇到这个奇怪的错误。这是在我的 read_orders() 函数中,它打开一个文件并创建订单对象。错误是:

`error: invalid use of incomplete type 'class Payment'
     theString += payment->print_detail();
                         ^~
.\groceries.cpp:11:7: note: forward declaration of 'class Payment'
 class Payment;`

根据我的理解,编译器看不到 Payment 对象的完整定义。我不明白这是为什么。另外,是的,我的教授希望将所有内容都放在一个 cpp 文件中...

这是我的代码

```
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>

class Order;
class Payment;
class Credit;

//GLOBAL VARIABLES
vector<Order> orders = {}; //gets filled from orders.txt file
vector<int> customers = {}; //keeps track of customers created

class Order
{
    friend class Payment;

private:
    int order_id;
    std::string order_date;
    int cust_id;
    Payment *payment;

public:
    Order(int theOId,std::string theDate,int theCId,Payment *thePayment) : order_id(theOId),order_date(theDate),cust_id(theCId),payment(thePayment){};
    ~Order()
    {
        delete &order_id,&order_date,&cust_id,payment;
    };

    double total() {}

    bool customerExists(int cust_id)
    {
        for (int &temp_cust_id : customers)
        {
            if (cust_id == temp_cust_id)
            {
                return true;
            }
        }
        return false;
    }

    std::string print_order()
    {
        std::string theString = "===========================\norder #" + std::to_string(order_id) + ",Date: " + order_date + "\n";
        theString += payment->print_detail();
        return theString;
    }
};


class Payment
{
private:
    double amount;

public:
    Payment(double amt = 0) : amount(amt){};
    virtual ~Payment()
    {
        delete &amount;
    };

    virtual std::string print_detail()
    {
        std::string theString = " Amount: " + std::to_string(amount); //std::to_string(get_amount());
    }
};


class Credit : public Payment //type 1
{
private:
    std::string card_number;
    std::string expiration;

public:
    Credit(std::string the_card_number,std::string theExpiration) : card_number(the_card_number),expiration(theExpiration),Payment(){};
    ~Credit()
    {
        delete &card_number,&expiration;
    }

    std::string print_detail() override
    {
        std::string theString = Payment::print_detail();
        theString += ",Paid by Credit card " + this->card_number + ",exp. " + this->expiration + "\n";
        return theString;
    }
};


void read_orders(std::string theFile)
{
    std::ifstream ifs;
    ifs.open(theFile);
    std::string s;
    std::vector<std::string> order;

    while (getline(ifs,s))
    {
        std::vector<LineItem> line_item = {};
        Payment *pptr = new Payment;

        order = split(s,','); //split just takes out the commas
        int theOrderId = std::stoi(order[1]);
        int theCustId = std::stoi(order[0]);
        std::string theDate = order[2];

        getline(ifs,s);    //Gets next line in orders.txt,This is the payment line
        order = split(s,');
        std::string paymentType = order[0];
        if (paymentType == "1")
        {
            pptr = new Credit(order[1],order[2]);
        }
        else if (...){}
    }
    ifs.close();
}


int main()
{
    read_customers("customers.txt");
    read_items("items.txt");
    read_orders("orders.txt");

    std::ofstream ofs;
    ofs.open("order_report.txt");

    for (auto &order : orders)
    {
        ofs << order.print_order() << std::endl;
    }

    ofs.close();
}
```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)