来自多个班级的Ostream

时间:2012-12-01 21:36:09

标签: c++ ostream

我似乎得到错误C2440:'return'无法从'LinkList'转换为'Tray *'。我想要做的是将第二类的输出添加到客户类的输出。因此,如果我要调用诸如whoIsEating()这样的方法,我可以轻松获得一个列表,列出了谁,在哪里以及什么。我有一个使用'朋友'的想法,但我对此非常不确定。

Customer.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);

customer.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;

Tray.cpp

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

怀疑的输出就像

姓名:John Smith

表编号:12

膳食详情:

饮料:茶

食物:鱼类和鱼类芯片

2 个答案:

答案 0 :(得分:2)

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

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

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

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

你需要返回一个指向托盘的指针,这样你可能想要做更多的事情:

Tray* Customer::getTray()
{
    return &(myTray->first());
}

答案 1 :(得分: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类不会公开您想要的数据。

您想要返回指向Tray的指针,a.k.a。Tray*

Tray* Customer::getTray()

...但你拥有的对象是指向LinkedList<Tray>

的指针
LinkList<Tray> *myTray; 

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

Node<T> *startingPtr;

... Node类可让您通过Tray方法访问getData个对象:

T getData();

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

解决方案

一种解决方法是让getTray返回您有权访问的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;
}