Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 返回指向类函数的指针_C++_Pointers_Return - Fatal编程技术网

C++ 返回指向类函数的指针

C++ 返回指向类函数的指针,c++,pointers,return,C++,Pointers,Return,我需要返回一个指向类函数的指针,该类函数用于打印该类中的一个数据成员。我必须遍历一个链表,以找到名称与用户搜索的名称匹配的节点。我不允许修改find函数的参数,也不允许修改名称的输出方式 以下是查找用户输入的名称的函数: Winery * const List::find(const char * const name) const { Node *traverse; traverse = headByName; while (traverse) {

我需要返回一个指向类函数的指针,该类函数用于打印该类中的一个数据成员。我必须遍历一个链表,以找到名称与用户搜索的名称匹配的节点。我不允许修改
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
对象的指针。您希望它打印出什么?我需要它打印出
酒庄项目中包含的
名称
。除了通过函数
item.getName()
之外,我没有访问
name
的权限。您应该将函数声明为返回
char*
,而不是
Winery*
。您不是因为
return
语句中的类型不匹配而收到错误吗?我猜您实际上想要
return-traverse->item并编写一个运算符或EverMind,修复了它!现在一切都很好。谢谢大家!
// 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;
};
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;
}