c++ - 如何实现返回 protected 结构的私有(private)函数

标签 c++

所以我想做的是我想要一个类 LinkedList ,并且在头文件中我有一个 protected 结构 ListNode ,它有一个字符串值参数和指向下一个节点的指针。在我的公共(public)成员函数中,我有一个添加函数,它调用递归的私有(private)添加函数。
这是 H 文件。

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED
#include <iostream>
using namespace std;
class LinkedList
{
protected:   // Declare a class for the list node.
struct ListNode
{
   string value;
   ListNode *next;
   ListNode(string value1, ListNode *next1 = NULL)
   {
      value = value1;
      next = next1;
   }
};
ListNode *head;                  // List head pointer
public:
    LinkedList() { head = NULL; }   // Constructor
    ~LinkedList();                  // Destructor
    void add(string value) { head = add(head, value);}
private:
    // Recursive implementations
    ListNode *add(ListNode *aList, string value);   //Function called by the void add function in public
};
#endif // LINKEDLIST_H_INCLUDED

当我尝试实现这个递归添加函数时,问题就出现了。我已经尝试了下面的代码,但我不断收到 ListNode 没有类型 的错误。即使我已将其定义为 protected 属性?
这是实现文件。

#include "LinkedList.h"
//Destructor works fine, nothing wrong here and we are using ListNode
LinkedList::~LinkedList()
{    ListNode *garbage;
    head = garbage;
    delete garbage;
}
//Error comes from this recursive add function
ListNode LinkedList::*add(ListNode *aList, string val)
{
    //add a node recursively
}

非常感谢任何有关我需要更改的内容的帮助。

(注意)如果您需要更多信息,我很乐意发布。我只是想要一些帮助。谢谢!

最佳答案

LinkedList::ListNode* LinkedList::add(ListNode *aList, string val)

您需要按照 LinkedList 正确确定 ListNode* 的范围,因为它是在类声明中声明的。

关于c++ - 如何实现返回 protected 结构的私有(private)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52470793/

相关文章:

c++ - 使用 g++ 编译器将目标文件生成到单独的目录 - C++

c++ - 如何关闭代码块中的所有自动更正/自动完成功能?

C++:如何将十六进制字符转换为无符号字符?

c++ - mbstowcs 在 Red Hat Linux 上返回 -1,但在 Solaris 上不返回

c++ - 将 zlib 与 const 数据一起使用

c++ - Boost asio http截止日期错误?

类定义中结构数组的 C++ 问题

c++ - 保存结果时按分隔符拆分 char 数组?

c++ - Qt - Visual Studio - 在多台计算机上处​​理项目

c++ - 我如何在 C++ 的 Caesar Cipher 程序中包含空格?