【问题标题】:Formating a Function that Returns a Pointer to a Contained Class?格式化返回指向包含类的指针的函数?
【发布时间】:2017-10-19 15:02:09
【问题描述】:

我正在尝试使用指向节点的指针的链接列表。 List 是 Node 的容器类。我正在尝试为列表格式化一个查找函数,该函数在链接列表中找到该节点时返回指向该节点的指针。但是,我不断收到错误消息,说没有类型说明符。 (错误显示在下面链接的屏幕截图中,主要查看 Node.h 中的第 10 行和第 17 行)。如何正确格式化以消除错误? https://imgur.com/vicL8FS

NODE.H //Both class declarations contained here
class list //container class
{
public:
    list();
    ~list();
    void insert(string f, string l, int a);
    node *find(string first, string last); //Pointer to contained class
private:
    node *head; //errors here
    int length;
};
class node
{
    friend list;
public:
    node();                           // Null constructor
    ~node();                          // Destructor 
    void put(ostream &out);           // Put
    bool operator == (const node &);  // Equal
private:
    string first, last;
    node *next;
};

NODE.CPP

#include "Node.h"
   node list::*find(string first, string last) 
    {
        return NULL; //logic not written yet
    }
//MAIN
p = a.find(first, last); //p is a pointer to node, a is a list.

【问题讨论】:

  • 试试node *list::find(string first, string last)
  • 那行不通。它只是增加了另一个错误
  • 可能,但node *list::find(string first, string last) 无效。因为返回类型是node *,函数名是list::find。你不能混合它们。
  • 请将错误连同问题一起粘贴到此处。不要链接屏幕截图。这将有助于在一个视图中更好地理解您的问题。
  • 我刚刚将您的新声明与下面列出的其他答案结合起来,错误已清除!

标签: c++ function class pointers containers


【解决方案1】:

现在node 是在list 之后定义的,当编译器开始解析list 类主体时,名称node 是未知的。您需要添加前向声明:

class node;

class list
{
...

所以node 将被正确识别为类型名称。

【讨论】:

  • 这似乎清除了错误。为了清楚起见,这不会重新定义任何变量吗?而且我不必将整个列表定义移到节点定义下面?
  • 不,这不会重新定义任何东西。前向声明仅使 node 可识别为类型名称。将整个 list 定义移动到 node 定义下方将是修复错误的另一种方法。请注意,friend list; 声明不需要 list 已经声明。
【解决方案2】:

错误的原因是编译器在编译类list的定义时不知道node这个名字是如何声明的。

有几种方法可以解决问题。您可以使用详细的类名。

例如,您可以首先定义类节点并使用类列表的详细名称。

class node
{
    friend class list;
public:
    node();                           // Null constructor
    ~node();                          // Destructor 
    void put(ostream &out);           // Put
    bool operator == (const node &);  // Equal
private:
    string first, last;
    node *next;
};

class list
{
    //...
};

或者您可以在类list 的定义中使用类node 的详细名称。例如

class list //container class
{
public:
    list();
    ~list();
    void insert(string f, string l, int a);
    class node *find(string first, string last); //Pointer to contained class
private:
    class node *head;
    int length;
};

class node
{
    //...
};

或者您可以使用 node 类的前向声明。

class node;

class list //container class
{
public:
    list();
    ~list();
    void insert(string f, string l, int a);
    node *find(string first, string last); //Pointer to contained class
private:
    node *head;
    int length;
};

class node
{
    //...
};

或者您可以使用 list 类的前向声明。

class list;

class node
{
    friend list;
public:
    node();                           // Null constructor
    ~node();                          // Destructor 
    void put(ostream &out);           // Put
    bool operator == (const node &);  // Equal
private:
    string first, last;
    node *next;
};

class list
{
    //...
};

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多