什么是这段代码中的错误/错误c ++告诉我纠正?

时间:2010-03-16 20:06:14

标签: c++

这里的代码编译器打印错误: 132 C:....`createlist'未声明(首先使用此功能) (每个未声明的标识符仅针对它出现的每个函数报告一次。)

并在main函数的所有调用中再次重复:( 有什么问题 ?? plzzzz帮帮我

#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;
}

4 个答案:

答案 0 :(得分:3)

createlistNode类的一种方法,但在main()内,您将其称为函数。我建议将Node视为C结构,并将这些方法实现为像Thomas提到的struct这样的函数,无论如何都是代码的结构,或reading a tutorial on C++ classes

答案 1 :(得分:1)

我的猜测是你错过了节点结构的结束'}':

template <typename T>
struct Node
{

T num;
struct Node<T> *next;

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

答案 2 :(得分:0)

createlist被定义为采用Node<T> *p的参数,但您传递的是struct Node<int>*的实例

答案 3 :(得分:0)

createlist是Node的成员方法。您正在尝试从main访问Node :: createlist。您无法执行此操作(即使您在调用中添加“Node ::”作用域),因为createlist不是静态方法。

将其更改为:

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

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

或者将createlist完全从Node类中拉出来,并使其成为自己的函数。