遇到 unexpected type 不懂什么意思,求教!!

hello_tony 2009-12-20 04:57:25
写一个一个链表的程序:

LinearList.h 如下

#include<iostream>

class ListNode
{
public:
int key;
ListNode *next;
ListNode():next(NULL){}
ListNode(int x):key(x),next(NULL){}
~ListNode();

};

class LinearList
{

private:
int size;
ListNode *head;

public:
LinearList(void) ;
~LinearList(void);
bool insert(int x, int pos);
bool remove(int &x, int pos);
int element(int pos) const;
int search(int x) const;
int length() const;
(ListNode*) find(int i)const;
};

LinearList.cpp 如下

#include"LinearList.h"


LinearList::LinearList(void)
{
size = 0;
head = new ListNode();
head->next = NULL;
}

(ListNode*) LinearList::find(int i) //此处有错,见下面的error
{
ListNode *node = head;
int x = 0;
while ((x++<i)&&(node!=NULL))
node = node->next;
return node;
}

bool LinearList::insert(int x, int pos)
{
if(x<0 || x>size)
{
std::cout<<"ArrayOutOfBound!";
return false;
}

ListNode *node = new ListNode(x);
ListNode *cur = find(pos);
node->next = cur->next;
cur->next = node;
size++;
return true;
}

bool LinearList::remove(int &x, int pos)
{
if(x<=0 || x>size)
{
std::cout<<"ArrayOutOfBound!";
return false;
}

ListNode *pre = find(pos-1);
ListNode *del = pre->next;
x = del->key;
pre->next = del->next;
delete del;
size--;
return true;

}

int LinearList::element(int pos) const
{
if(pos<=0 || pos>size)
{
std::cout<<"ArrayOutOfBound!";
return 0;
}

ListNode *cur;
cur = find(pos);
return cur->key;
}

//查找值为x的元素,返回元素的位置(第一个元素的位置为1)。未找到时返回0。
int LinearList::search(int x) const
{
int i = 1;
ListNode *itr = head->next;
while(itr!=NULL && itr->key!=x)
{
itr = itr->next;
i++;
}
return i;
}

inline int LinearList::length() const
{
return size;
}



编译报错:
error C2226: syntax error : unexpected type 'ListNode'
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)

小弟学习c++不久,望高手不吝赐教~~~~
...全文
2020 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hello_tony 2009-12-20
  • 打赏
  • 举报
回复
多谢各位大哥了~~程序终于搞好了~~

谢谢!!
cattycat 2009-12-20
  • 打赏
  • 举报
回复
(ListNode*) find(int i)const;

不要加括号,改成
ListNode* find(int i)const;
函数实现前面也不要加括号,最后还得加上 using namespace std;
la_feng 2009-12-20
  • 打赏
  • 举报
回复
using namespace std;
mstlq 2009-12-20
  • 打赏
  • 举报
回复
单文件版……
没问题……
楼主不会抄错了吧?

#include <iostream>
using namespace std;
class ListNode
{
public:
int key;
ListNode *next;
ListNode():next(NULL){}
ListNode(int x):key(x),next(NULL){}
~ListNode(){};

};

class LinearList
{

private:
int size;
ListNode *head;

public:
LinearList(void) ;
~LinearList(void);
bool insert(int x, int pos);
bool remove(int &x, int pos);
int element(int pos) const;
int search(int x) const;
int length() const;
ListNode* find(int i)const;
};


LinearList::LinearList(void)
{
size = 0;
head = new ListNode();
head->next = NULL;
}

ListNode* LinearList::find(int i)const
{
ListNode *node = head;
int x = 0;
while ((x++ <i)&&(node!=NULL))
node = node->next;
return node;
}

bool LinearList::insert(int x, int pos)
{
if (x <0 || x>size)
{
std::cout <<"ArrayOutOfBound!";
return false;
}

ListNode *node = new ListNode(x);
ListNode *cur = find(pos);
node->next = cur->next;
cur->next = node;
size++;
return true;
}

bool LinearList::remove(int &x, int pos)
{
if (x <=0 || x>size)
{
std::cout <<"ArrayOutOfBound!";
return false;
}

ListNode *pre = find(pos-1);
ListNode *del = pre->next;
x = del->key;
pre->next = del->next;
delete del;
size--;
return true;

}

int LinearList::element(int pos) const
{
if (pos <=0 || pos>size)
{
std::cout <<"ArrayOutOfBound!";
return 0;
}

ListNode *cur;
cur = find(pos);
return cur->key;
}

//查找值为x的元素,返回元素的位置(第一个元素的位置为1)。未找到时返回0。
int LinearList::search(int x) const
{
int i = 1;
ListNode *itr = head->next;
while (itr!=NULL && itr->key!=x)
{
itr = itr->next;
i++;
}
return i;
}

inline int LinearList::length() const
{
return size;
}

int main(int argc, char **argv)
{

return 0;
}
hello_tony 2009-12-20
  • 打赏
  • 举报
回复
谢谢~都照改了,可是错误仍然存在啊~~

继续求教~~~
mstlq 2009-12-20
  • 打赏
  • 举报
回复
三处改动……

class ListNode
{
public:
int key;
ListNode *next;
ListNode():next(NULL){}
ListNode(int x):key(x),next(NULL){}
~ListNode(){};//mark
};



class LinearList
{

private:
int size;
ListNode *head;

public:
LinearList(void) ;
~LinearList(void);
bool insert(int x, int pos);
bool remove(int &x, int pos);
int element(int pos) const;
int search(int x) const;
int length() const;
ListNode* find(int i)const; //mark
};


ListNode* LinearList::find(int i) const //mark
{
ListNode *node = head;
int x = 0;
while ((x++ <i)&&(node!=NULL))
node = node->next;
return node;
}

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧