C++ 此代码中的错误/错误是什么+;告诉我更正的地方?
在此代码中,编译器打印错误: 132 C:`createlist'未声明(首先使用此函数) (每个未声明的标识符对于其出现的每个函数只报告一次。) 并在main function中的所有调用中重复该操作:( 有什么问题吗?请帮帮我C++ 此代码中的错误/错误是什么+;告诉我更正的地方?,c++,C++,在此代码中,编译器打印错误: 132 C:`createlist'未声明(首先使用此函数) (每个未声明的标识符对于其出现的每个函数只报告一次。) 并在main function中的所有调用中重复该操作:( 有什么问题吗?请帮帮我 #include<iostream> #include<string> using namespace std; template <typename T> struct Node { T num; struct Node<
#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;
}
#包括
#包括
使用名称空间std;
模板
结构体类型
{
T数;
结构节点*下一步;
//设置列表节点的步骤
void createlist(节点*p)
{T数据;
for(;;)//它的容器,直到用户想要停止为止
{coutdata;
如果(数据='#')
{p->next=NULL;
打破
}
其他的
{p->num=数据;
p->next=新节点;
p=p->next;
}
}
}
//计数列表以在排序函数中使用它
整数计数列表(节点*p)
{
整数计数=0;
while(p->next!=NULL)
{count++;
p=p->next;
}
返回计数;
}
//排序表
无效排序(节点*p)
{Node*p1,*p2;//要在它们之间进行比较的元素1和2
inti,j,n;
温度;
n=计数表(p);
对于(i=1;不精确;
对于(j=1;jnum>p2->num)
{temp=p2->num;
p2->num=p1->num;
p1->num=温度;
}
}
p1=p1->next;
p2=p2->next;
}
}
//在用户选择的任何位置添加新号码
void insertatloc(节点*p)
{tn;//读取新数值
int loc;//读取所选位置
节点*locadd、*newnum、*temp;
cout-loc;
locadd=NULL;//将其设为NULL,以检查从用户ot not读取后是否有位置
while(p->next!=NULL)
{if(p->next==loc)
{locadd=p;
打破
}
p=p->next;
}
if(locadd==NULL)
{coutnext;
locadd->next=newnum;
newnum->next=temp;
}
locadd->num=sort(locadd);//调用排序函数
}
//显示所有列表节点
无效显示列表(节点*p)
{
while(p->next!=NULL)
{
coutcreatelist
被定义为获取一个Node*p
的参数,但您正在向它传递一个struct Node*
createlist
被定义为获取一个Node*p
的参数,但您正在向它传递一个struct Node*
的实例,我猜您缺少一个结束“}'对于您的节点结构:
template <typename T>
struct Node
{
T num;
struct Node<T> *next;
}; // <--- add this line.
模板
结构体类型
{
T数;
结构节点*下一步;
};//我猜您的节点结构缺少一个结尾“}”:
template <typename T>
struct Node
{
T num;
struct Node<T> *next;
}; // <--- add this line.
模板
结构体类型
{
T数;
结构节点*下一步;
};//createlist
是节点
类的方法,但在main()中
您将其称为函数。我建议将节点
视为C-struct,并将这些方法实现为函数,采用Thomas提到的结构
,这就是代码的结构,或者。createlist
是节点
类的方法,但在main()
您将其作为函数调用。我建议将节点
视为C结构,并将这些方法实现为函数,采用Thomas提到的结构
,这就是您的代码的结构,或者。createlist是Node的成员方法。您正在尝试从main访问Node::createlist。您不能这样做(即使在调用中添加“Node::”作用域),因为createlist不是静态方法
将其更改为:
// to create list nodes
static void createlist(Node<T> *p)
//创建列表节点的步骤
静态void createlist(节点*p)
以及:
//在mynodes列表中创建节点
coutcreatelist是Node的成员方法。您正在尝试从main访问Node::createlist。您无法执行此操作(即使在调用中添加“Node::”作用域),因为createlist不是静态方法
将其更改为:
// to create list nodes
static void createlist(Node<T> *p)
//创建列表节点的步骤
静态void createlist(节点*p)
以及:
//在mynodes列表中创建节点
cout@jeje欢迎使用Stack Overflow。为了获得最佳效果,请在将来格式化您的代码。您可以查看我对您的帖子所做的更改,以便在将来访问时获得正确的格式,也请缩进您的代码。这不会造成伤害。您得到了多少?您的成员函数不需要将节点
作为参数eter,因为从概念上讲,该函数已经是实例的一部分;直接对成员进行操作。@jeje欢迎使用Stack Overflow。为了获得最佳效果,请在将来格式化您的代码。您可以查看我对您的文章所做的更改,以便在将来访问时获得正确的格式,也请缩进您的代码。这不会有什么影响。如何处理这大部分都是给您的?您的成员函数不需要将节点
作为参数,因为从概念上讲,该函数已经是实例的一部分;直接对成员进行操作。