0

Here is the header file

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);

This is how the cc file will be

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;

}
}

The error says that the head is not declared in the function q_folder. What causes that and how do I solve it?

Bart
  • 19,692
  • 7
  • 68
  • 77
Pcmsr
  • 1
  • 4

2 Answers2

5

In your method, q-folder, you call insertAfter with "&head". The only way head could be recognizable in any class method is if it were

  1. Local
  2. Parameter
  3. Class member (inherited or otherwise)
  4. Global (however, consider this a no-no)

Seeing how it is none of the three, it doesn't know what you mean by "&head". Plus as hmjd correction mentioned, none of your declared methods are labeled as belonging to that class. You wrote them as if they were separate methods independent from any class. In order to indicate that they belong to class sv1, you need to add "sv1::" to the beginning of the method name.

Neil
  • 5,762
  • 24
  • 36
  • The head in q_folder should reptesent Null first time then it should represent the previous folder everytime i call insert after again.. – Pcmsr Sep 07 '12 at 08:08
  • sorry I missed typing sv1:: in all the member function. – Pcmsr Sep 07 '12 at 08:08
  • More over I call q_folder many times from some other functions of the same class also. so How do i manage to get a perfect linked list? – Pcmsr Sep 07 '12 at 08:11
  • OK i found out that i should declare the Node head in public: in the class. thanks anyways :) – Pcmsr Sep 07 '12 at 08:14
  • @Pcmsr, if I may offer you some advice. If you really want to get in some practical experience programming, you should abstract the interface such that you don't have to know *how* it's implemented. In other words, you have methods "add" and "remove" which only takes strings, and your implementation does the details. – Neil Sep 07 '12 at 10:08
0

I think you are pursuing a wrong pattern. Why have you defined Node struct inside private section of the class !? The usual pattern is that you have to separate classes for Node and LinkedList itself. Please create another class which encapsulate your node. Also you can implement it using templates to support different data types.

Also head is an internal variable. You shouldn't accept it as a function argument. This is not object oriented because your linked list (if it works) is not consistent and all defining variables are open to alter. You need you make head private for a LinkedList. Here you can find some examples :

Simple C/C++ Linked List

How to create Linked list using C/C++

Community
  • 1
  • 1
Kamran Amini
  • 1,062
  • 8
  • 14