首页 > 解决方案 > 为什么要从“Class”转换为“Class*”?

问题描述

我想道歉,因为我知道这个问题已经被问过一千次了。我已经阅读了所有我能找到的类似帖子,但我没有找到我的答案。

我正在开发一个程序的第一部分,该程序创建一个餐厅顾客队列,如果顾客选择接受促销材料,则将顾客推到一个堆栈上。我还没有开始为堆栈编码。现在我只是想让它把数据文件加载到队列中,这是一个链表,然后打印数据。由于这个错误,它不会编译。

错误是:

错误:无法将“客户”转换为“客户 *”
newNode->data = aCustomer

这是我的 .h 文件

#ifndef QUEUE_H                                                        
#define QUEUE_H                                                        
                                                                       
#include <iostream>                                                    
#include <cstring>                                                     
#include <fstream>                                                     
                                                                       
using namespace std;                                                   
const int CAP = 100;                                                   
class Queue;                                                           
                                                                       
class Customer                                                         
{                                                                      
public:                                                                
        Customer();                                                    
        Customer(Customer& aCustomer);                                 
        Customer(char tempGroupName[], int& tempGroupSize, char tempSpecialSeating[], bool& tempWantsPromo);                                   
        ~Customer(); 
        void openFile(ifstream& infile);                               
        const Customer& operator=(const Customer& aCustomer);          
//      friend ostream& operator<<(ostream& cout, const Queue& aQueue);
        void setGroupName(char *tempGroupName);
        void setGroupSize(int &tempGroupSize);
        void setSpecialSeating(char * tempSpecialSeating);             
        void setWantsPromo(bool &tempWantsPromo);
private:
        char * groupName;                                              
        int groupSize;
        char * specialSeating;                                         
        bool wantsPromo;
};      

class Queue                                                            
{
public:                                                    
        Queue();                                                       
        Queue(Queue& aQueue);
        ~Queue();
        friend ostream& operator<<(ostream& cout, const Queue& aQueue);
        void loadQueue(ifstream& infile, Customer& aCustomer);
        void addCustomer(Customer& aCustomer);
        void enqueue(Queue& aQueue);
        void dequeue(Queue& aQueue);
        void peek();
        void displayList();
        void displayWait();
        void isEmpty();
        bool promotions();
        void mostRecent();

private:
        struct Node
        {
                Node(Customer& aCustomer)
                {
                        data = new Customer(aCustomer);
                //      Node * next = nullptr;
                //      Node * prev = nullptr;
                }
                Customer * data;
                Node *prev, *next;

        };
        Node * head;
        Node * tail;
        int lineSize;
};
#endif

这是我在第二行“newNode->data = aCustomer”上收到错误的函数

void Queue::addCustomer(Customer& aCustomer)                           
{       
        Node* newNode;
        newNode->data = aCustomer;                                     
        newNode->next = nullptr;                                       
        
        if(head == nullptr)                                            
        {       
                head = newNode;                                        
                tail = newNode;                                        
        }
        else                                                           
        {       
                tail->next = newNode;                                  
                tail = newNode;                                        
        }                                                              
}

我的理解是编译器期待一个指针?但我不明白。

为什么它会认为它应该是一个指针?是因为 newNode 是一个指针吗?

我尽量不发布代码墙,但如果需要,我很乐意发布更多内容。

标签: c++

解决方案


您的问题可以简化为:

class Customer{};

int main()
{
    Customer cust;
    Customer &cust_ref = cust;
//  Customer *cust_ptr = cust_ref;   // Error These are different types
    Customer *cust_ptr = &cust_ref;  // This is ok
}

在您的代码中,newNode->data 是一个customer *(指针),aCustomer 是一个Customer &(参考)。要使该作业生效,请将其更改为:

newNode->data = &aCustomer; // Take the address with &

推荐阅读