gpt4 book ai didi

c++ - 此代码中的错误/错误是什么 C++ 告诉我更正?

转载 作者:行者123 更新时间:2023-11-27 23:33:59 25 4
gpt4 key购买 nike

在此代码中,编译器打印错误:132 C:.... `createlist' undeclared (第一次使用这个函数)(每个未声明的标识符在它出现的每个函数中只报告一次。)

并在主函数的所有调用中再次重复它:(有什么问题 ?? 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;
}

最佳答案

createlist 是您的 Node 类的一个方法,但在 main() 中您将其作为函数调用。我建议将 Node 视为 C 结构并将这些方法实现为采用 struct 的函数,就像 Thomas 提到的那样,这就是您的代码的结构方式,或者 reading a tutorial on C++ classes .

关于c++ - 此代码中的错误/错误是什么 C++ 告诉我更正?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2457704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com