C++程序问题,求大神帮我看下哪里错了

2025-03-06 21:54:06
推荐回答(3个)
回答1:

错误的意思是说你的声明和定义不一样,但仔细看了,你写了没错的。
程序中应该不是这一个错误,LinkList::Locate函数中“p=first-next;count=1;”中少了一个箭头,刚才我在VS2008中调试了下,按照提示做了修改,主函数名和参数用了不支持unicode的main(_tmain是main的别名),头文件#include "stdafx.h"也删了,能正常运行,代码如下:
#include
#include
#define NULL 0
using namespace std;
template
struct Node{
DataType data;
Node *next;
};
template
class LinkList
{
public: LinkList();
LinkList(DataType a[],int n);
~LinkList();
int Length();
DataType Get(int i);
void PrintList();
int Locate(DataType x);
private: Node *first;
};
template
LinkList::LinkList()
{
first=new Node;
first->next=NULL;
}
template
LinkList::LinkList(DataType a[],int n)
{
Node *first,*s,*p;
first=new Node;
first->next=NULL;
int i;
this->first = first;
for(i=0;i {
s= new Node;
s->data=a[i];
s->next=first->next;
first->next=s;
}
}
template
LinkList::~LinkList()
{
Node *q;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template
void LinkList::PrintList()
{
Node *p;
p=first->next;
while(p!=NULL)
{
cout<data;
p=p->next;
}
}
template
int LinkList::Length()
{
Node *p;
int count;
p=first->next;
count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}template
int LinkList::Locate(DataType x)
{
Node *p;
int count;
p=first->next;count=1;
while(p!=NULL)
{
if(p->data==x)
return count;
p=p->next;
count++;
} return 0;
}
int main()
{
int b[3];
b[0]=2,b[1]=4,b[2]=6;
LinkList a(b,3);
a.PrintList();
cout< a.Locate(6);
return 0;
}

回答2:

我试了一下,没有上面你的那个错误,不过有其他4个错误(可能与编译器有关)

  1.  #include "stdafx.h"这句话删去,不做MFC,用它没什么用,而且我的编译器(VS2012)报错

  2. void PrintList(); 这句你没有写在类里面,只是在外面实现了,要加到LinkList类定义里面。

  3. LinkList::LinkList() ;(第一个) 在它的上面加  template

  4. int LinkList::Locate(DataType x)这个函数里面p=first->next; 这句话你写成了

    p=first-next; 少了一个>,你把"->" 写成 “-” 了。

回答3:

某个地方错了