c++ - 类中的链表,应通过类中的函数调用

标签 c++ linked-list

这是头文件

class sv1{
private:
  struct Node{
          string title;
          Node* next;
              };
public:
void InsertAfter(Node **head, string title);
void getMetadata(string t);
void q_folder(string t_q);

这是cc文件的样子

void sv1::getMetadata(string t)
{
Node *head=NULL;
title=t;
q_folder(title);
}
void sv1::q-folder(string t_q)
{
InsertAfter(&head,t_q);
}
void sv1::insertAfter(Node **head,string title)
{
if (head==NULL)
{
Node* temp=NULL;
temp=new Node;
temp->title=title;
*head=temp;
}
else{
Node *temp= new Node;
temp= new Node;
temp->title=title;
temp->next=(*head)->next;
(*head)->=temp;

}
}

错误说 head 没有在函数 q_folder 中声明。是什么原因造成的,我该如何解决?

最佳答案

在您的方法 q-folder 中,您使用“&head”调用 insertAfter。在任何类方法中 head 可以被识别的唯一方法是如果它是

  1. 本地
  2. 参数
  3. 类成员(继承或其他方式)
  4. 全局(但是,认为这是一个禁忌)

看它怎么都不是这三个,它不知道你说的“&head”是什么意思。另外,如 hmjd 更正所述,您声明的方法均未标记为属于该类。您将它们编写为就好像它们是独立于任何类的独立方法一样。为了表明它们属于类sv1,需要在方法名的开头加上“sv1::”。

关于c++ - 类中的链表,应通过类中的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12314009/

相关文章:

c++ - 我正在尝试为正方形设置棋盘纹理

c++ - 如何正确销毁链表?

c++ - 函数模板显式特化 C++

c - 反向双链表

c - 使用递归在字符链接列表中搜索元素(使用随机数生成器插入字符)的程序

c++ - 在 C++ 中,为什么要求列表在合并之前必须排序

c++ - 将字符串解析为int

c++ - 是否可以将对象分配给 int?

c++函数返回带偏移量的数组

c++ - Makefile 错误 - 链接器输入文件未使用