返回指向类函数的指针

时间:2014-10-17 04:12:27

标签: c++ pointers return

我需要返回一个指向类函数的指针,该函数用于打印该类中的一个数据成员。我必须遍历链表才能找到名称与用户搜索名称相匹配的节点。我不允许修改find函数的参数,我不允许修改输出名称的方式。

以下是查找用户输入名称的函数:

Winery * const List::find(const char * const name) const
{
    Node     *traverse;

    traverse = headByName;
    while (traverse)
    {
        if (strcmp(name, traverse->item.getName()) == 0)
            return &(traverse->item.getName());               //getName just returns the name of the item
        else
            traverse = traverse->nextByName;
    }
    return 0;
}

以下是main内的电话:

//I'm not allowed to change ANY of this
cout << endl << ">>> search for \"Gallo\"" << endl << endl;
wPtr = wineries->find("Gallo");
if (wPtr != 0)
    cout << wPtr;
else
    cout << "not found" << endl;

我写的不会编译。我找不到编写它的方法,以便编译并实际输出函数的名称。

编辑:这是Winery的头文件:

// make NO CHANGES to this file

#pragma once                // include this .h file file only once

#include <ostream>

class Winery
{
public:

    Winery(const char * const name, const char * const location, const int acres, const int rating);
    virtual ~Winery(void);

    // nothing needed in the .cpp file for these functions - complete implementation is provided
    const char * const getName() const { return name; }
    const char * const getLocation() const { return location; }
    const int getAcres() const { return acres; }
    const int getRating() const { return rating; }

    // print out column headings for lists of wineries, as specified by lab1output.txt
    static void displayColumnHeadings(std::ostream& out);

    // print out a winery, as specified by lab1output.txt
    friend std::ostream& operator<<(std::ostream& out, Winery *w);

private:
    char    *name;
    char    *location;
    int     acres;
    int     rating;
};

这是wPtr的定义:

Winery  *wPtr;

第二次编辑:这是重载的<<运算符:

ostream& operator<<(ostream& out, Winery *w)
{
    out << left << setw(25) << *w->getName() << " " << setw(18) << *w->getLocation() << " " << setw(5) << w->getAcres() << " " << setw(6) << w->getRating() << endl;

    return out;
}

0 个答案:

没有答案