-2

我似乎收到了错误 C2440:'return' cannot convert from 'LinkList' to 'Tray *'。我想要做的是将第二类的输出添加到客户类的输出中。因此,如果我要调用诸如 whoIsEating() 之类的方法,我可以很容易地得到谁、在哪里和做什么的列表。我有一个使用“朋友”的想法,但我对此不太确定。

客户.h

class Customer
{
private:
    int tableNumber;
    std::string firstName;
    std::string lastName;
    LinkList<Tray> *myTray;
public:
    Customer();
    Customer(std::string sFirstName, std::string sLastName,
        int sTableNumber,
        LinkList<Tray> *myTray = new LinkList<Tray>());
    ~Customer(void);

    int gettableNumber();
    Tray* getTray(void);

客户.cpp

Tray* Customer::getTray()
{
    return *myTray;
}

ostream& operator << (ostream& output, Customer& customer)
{
    output << customer.getFirstName() << " " << customer.getLastName() << endl
        << customer.gettableNumber() << endl
        << (*customer.getTray()) << endl; //Edit: Fixed this
    return output;

托盘.cpp

ostream& operator << (ostream& output, Tray& tray)
{
    output << "Drink: " << tray.getdrink() << endl;
    output << "Food: " << tray.getfood() << endl;
    return output;
}

怀疑的输出就像

姓名:约翰·史密斯

桌数:12

用餐详情:

喝茶

食物:炸鱼薯条

4

2 回答 2

2

在您的 customer.cpp 文件中,您有 getTray 返回一个指向托盘的指针,并且您传递了 myTray 实例变量:

Tray* Customer::getTray()
{
    return *myTray;
}

但是,在您的客户类中,您已经将 myTray 定义为 LinkedList

class Customer
{
    ...
    LinkList<Tray> *myTray; << here

您需要返回一个指向托盘的指针,因此您可能想做更多类似的事情:

Tray* Customer::getTray()
{
    return &(myTray->first());
}
于 2012-12-01T21:43:12.843 回答
0

你发布了

template<typename T>
class LinkList {
public:
    LinkList();
    ~LinkList();
    void insert(T); //Insert new node in order
    void print(); //Outputs contents
    int getSize();
    void target(T);
    Node<T>* search(T); //Search for value in link list 
                        //and returns pointer to value
    Node<T> *startingPtr;
    Node<T> *endPtr;
private:
    bool isEmpty();
    void insertStart(T); //insert before first node
    void insertEnd(T); //insert after last node
    void insertNext(T);
    void insertBefore(T);
};

template<typename T>
class Node {
    friend class LinkList<T>;
public: 
    T getData();
    Node(T);
    Node* nextPtr;
private:
    T data;
    Node* prevPtr;
};

问题

该类LinkedList不会公开您想要的数据。

你想返回一个指向 a 的指针Tray,也就是Tray*

Tray* Customer::getTray()

...但是您拥有的对象是指向 a 的指针LinkedList<Tray>

LinkList<Tray> *myTray; 

LinkedList课程使您可以访问Node<Tray>*,例如:

Node<T> *startingPtr;

...并且该类使您可以通过以下方法Node访问Tray对象:getData

T getData();

...但不幸的是,您想要 a Tray*,并且从中获得 a 并不简单Tray*。(问题是它getData返回一个很快就会消失的临时对象,所以如果你用那个对象的地址来获取 a Tray*,你将有一个很快指向垃圾的指针。)

解决方案

一种解决方法是getTray返回 a Node<Tray>*,您可以访问它,例如:

Node<Tray>* Customer::getTray()
{
    return myTray->startingPtr;
}

// note that this assumes that the customer has at least one tray in it's list
ostream& operator << (ostream& output, Customer& customer)
{
    output << customer.getFirstName() << " " << customer.getLastName() << endl
        << customer.gettableNumber() << endl
        << customer.getTray()->getData() << endl;
    return output;
}

// note the added 'const', which means tray.getdrink() and tray.getfood()
// will have to be const member methods
ostream& operator << (ostream& output, const Tray& tray) 
{
    output << "Drink: " << tray.getdrink() << endl;
    output << "Food: " << tray.getfood() << endl;
    return output;
}
于 2012-12-02T15:11:28.340 回答