jeje
jeje

Reputation: 9

what is mistakes/errors in this code c++ tell me the correction?

here in this code the compiler print error : 132 C:.... `createlist' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.)

and repeat it again in all calls in main function :( what's the problem ?? plzzzz help me

#include<iostream>
#include<string>
using namespace std;

template <typename T>
struct Node
{

T num;
struct Node<T> *next;

// to craet list nodes
void createlist(Node<T> *p)
{ T data;
   for(    ;    ;    ) // its containue until user want to stop
    { cout<<"enter data number or '#' to stop\n";
     cin>>data;
      if(data == '#')
       { p->next =NULL;
                  break;
       } 
         else
                { p->num= data;
                  p->next = new Node<T>;
                  p=p->next;
                }
    }
}



//count list to use it in sort function
int countlist (Node<T> *p)
{
int count=0;
while(p->next != NULL)
    {  count++;
        p=p->next;
    }
     return count;
}

// sort list
void sort( Node<T> *p)
{ Node<T> *p1, *p2; //element 1 & 2 to compare between them
 int i, j , n;
 T temp;

n= countlist(p);
for( i=1; i<n ; i++)
{ // here every loop time we put the first element in list in p1 and the second in p2
p1=p;
p2=p->next;
   for(j=1; j<=(n-i) ; j++)
   {
     if( p1->num > p2->num)
     { temp=p2->num;
      p2->num=p1->num;
      p1->num=temp;
      } 
    }
   p1= p1->next;
   p2= p2->next;
}
}


//add  new number in any location the user choose
void insertatloc(Node<T> *p)
{ T n; //read new num
   int loc; //read the choosen location
   Node<T> *locadd, *newnum, *temp;

  cout <<" enter location you want ..! \n";
  cin>>loc;
  locadd=NULL; //make it null to checked if there is location after read it from user ot not
  while(p->next !=NULL)
  { if( p->next==loc)
        { locadd=p;
                break;
        }
     p=p->next;
  }

if (locadd==NULL)
{cout<<" cannot find the location\n";}
else //if location is right 
{cout<<" enter new number\n"; // new number to creat also new location for it
  cin>>n;
  newnum= new Node/*<T>*/;
  newnum->num=n;
  temp= locadd->next;
  locadd->next=newnum;
  newnum->next=temp;

 }
locadd->num=sort(locadd); // call sort function 
}




// display all list nodes

void displaylist (Node<T> *p)
{
while (p->next != NULL)
      {
        cout<<" the list contain:\n";
        cout<<p->num<<endl;
        p=p->next;
      }
}

};//end streuct

int main()
{

cout<<"*** Welcome  in Linked List Sheet 2****\n";

// defined pointer for structer Node
// that value is the address of first node


struct Node<int>*mynodes= new struct Node<int>;



// create nodes in mynodes list
cout<<"\nCreate nodes in list";
createlist(mynodes);


// insert node in location
insertatloc(mynodes);



/* count the number of all nodes
nodescount = countlist(mynodes);
cout<<"\nThe number of nodes in list is: "<<nodescount;*/



// sort nodes in list
sort(mynodes);



// Display nodes
cout<<"\nDisplay all nodes in list:\n";
displaylist(mynodes);


system("pause");
return 0;
}

Upvotes: 0

Views: 219

Answers (4)

Tim
Tim

Reputation: 9172

createlist is a member method of Node. You are attempting to access Node::createlist from main. You cannot do this (even if you add "Node::" scoping to your call), because createlist is not a static method.

Change it to:

// to create list nodes
static void createlist(Node<T> *p)

and:

// create nodes in mynodes list
cout<<"\nCreate nodes in list";
Node::createlist(mynodes);

Or pull createlist out of the Node class entirely, and make it into its own function.

Upvotes: 0

hao
hao

Reputation: 10238

createlist is a method of your Node class, but inside main() you're calling it as a function. I recommend either treating Node like a C-struct and implementing those methods as functions taking a struct like Thomas mentions, which is how your code is structured anyway, or reading a tutorial on C++ classes.

Upvotes: 3

Thomas Matthews
Thomas Matthews

Reputation: 57749

My guess is you are missing a closing '}' for your node structure:

template <typename T>
struct Node
{

T num;
struct Node<T> *next;

}; // <--- add this line.

Upvotes: 1

Justin Ethier
Justin Ethier

Reputation: 134257

createlist is defined to take a parameter of Node<T> *p but you are passing it an instance of struct Node<int>*

Upvotes: 0

Related Questions