c++编译错误,实在看不出来

hbxtght 2011-11-06 11:55:18
代码如下:
#include <iostream>
#include <string>
using namespace std;

template<class T>
class Node
{
public:
T Record;
Node<T>* Next;
Node()
{
Record = T();
Next = NULL;
}
Node(T record)
{
Record=record;
Next = NULL;
}
friend ostream& operator<< (ostream& ost, const Node<T>& node)
{
ost<<node.Record;
return ost;
}
};

template<class T>
class LinkedList
{
protected:
Node<T>* _head;
Node<T>* _rear;
public:
LinkedList()
{
_head = NULL;
_rear = NULL;
}
LinkedList<T>& operator= (const LinkedList<T>& list)
{
if(this == &list) //×Ô¸³Öµ
{
return *this;
}
else
{
//Clear
while(!IsEmpty())
{
DelFirst();
}

Node<T>* node = list._head;
while(node != NULL)
{
InsLast(node->Record);
node = node->Next;
}
return *this;
}
}
bool IsEmpty()
{
if(_head == NULL)
{
return true;
}
else
{
return false;
}
}
void InsFirst(T record)
{
Node<T>* node = new Node<T>(record);
if(IsEmpty())
{
_head = node;
_rear = node;
}
else
{
node->Next = _head;
_head = node;
}
}
void InsLast(T record)
{
Node<T>* node = new Node<T>(record);
if(IsEmpty())
{
_head = node;
_rear = node;
}
else
{
_rear->Next = node;
_rear = node;
}
}
T DelFirst()
{
if(!IsEmpty())
{
Node<T>* node = _head;
T result = node->Record;
if(_head == _rear)
{
_head = NULL;
_rear = NULL;
delete node;
return result;
}
else
{
_head = _head->Next;
delete node;
return result;
}
}
else
{
T result;
return result;
}
}
~LinkedList()
{
//cout<<"~Linked"<<endl;
while(!IsEmpty())
{
DelFirst();
}
}
LinkedList(LinkedList& list)
{
//cout<<"copy"<<endl;
if(list.IsEmpty())
{
_head = NULL;
_rear = NULL;
}
else
{
_head = NULL;
_rear = NULL;
Node<T>* node = list._head;
while(node != NULL)
{
InsLast(node->Record);
node = node->Next;
}
}
}
friend ostream& operator<< (ostream& ost, const LinkedList& list)
{
Node<T>* node = list._head;
while(node != NULL)
{
ost<<*node<<endl;
node = node->Next;
}
return ost;
}
};
template<class T>
class LinkedQueue:protected LinkedList<T>
{
public:
LinkedQueue():LinkedList<T>()
{
}
LinkedQueue(LinkedQueue& q)
{
this->_head = NULL;
this->_rear = NULL;
Node<T>* node = q._head;
while(node != NULL)
{
EnQue(node->Record);
node = node -> Next;
}
}
LinkedQueue<T>& operator= (const LinkedQueue<T>& q)
{
if(this == &q)
{
return *this;
}
else
{
while(!IsEmpty())
{
DeQue();
}
Node<T>* node = q._head;
while(node != NULL)
{
EnQue(node->Record);
node = node->Next;
}
return *this;
}
}

void EnQue(T record)
{
LinkedList<T>::InsLast(record);
}
T DeQue()
{
return LinkedList<T>::DelFirst();
}
bool IsEmpty()
{
return LinkedList<T>::IsEmpty();
}
friend ostream&operator<<(ostream&os,const LinkedQueue<T>& q)
{
Node<T>* node = q._head;
while(node != NULL)
{
os<<*node<<endl;
node = node->Next;
}
return os;
}
};

template<class T>
class LinkedStack:protected LinkedList<T>
{
public:
LinkedStack():LinkedList<T>()
{
}
LinkedStack(LinkedStack& s)
{
this->_head = NULL;
this->_rear = NULL;
Node<T>* node = s._head;
while(node != NULL)
{
InsLast(node->Record);
node = node->Next;
}
}
LinkedStack<T>& operator= (const LinkedStack<T>& s)
{
if(this == &s)
{
return *this;
}
else
{
while(!IsEmpty())
{
Pop();
}
Node<T>* node = s._head;
while(node != NULL)
{
InsLast(node->Record);
node = node->Next;
}
return *this;
}
}
void Push(T record)
{
LinkedList<T>::InsFirst(record);
}
T Pop()
{
return LinkedList<T>::DelFirst();
}
bool IsEmpty()
{
return LinkedList<T>::IsEmpty();
}
friend ostream& operator<< (ostream& ost, const LinkedStack<T>& stack)
{
Node<T>* node = stack._head;
while(node != NULL)
{
ost<<*node<<endl;
node = node->Next;
}
return ost;
}
};

int main()
{
LinkedQueue<string> q1;
q1.EnQue("a");
q1.EnQue("b");
q1.EnQue("c");
cout<<q1<<endl;

LinkedQueue<string> q2;
q2.EnQue("aa");
q2.EnQue("bb");
q2.EnQue("cc");
cout<<q2<<endl;

LinkedStack< LinkedQueue<string> > stackQue;
stackQue.Push(q1);
stackQue.Push(q2);
cout<<"stackQue "<<endl;
cout<<stackQue<<endl;
stackQue.Pop();

return 0;
}

编译不过去,搞不定了,求助
...全文
124 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbxtght 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 demon__hunter 的回复:]

T Pop()
{
return LinkedList<T>::DelFirst();
}
-->T DelFirst()
{
if(!IsEmpty())
{
Node<T>* node = _head;
T result = node->Record;
……
[/Quote]
原来是这样,谢谢啊!!!
机智的呆呆 2011-11-07
  • 打赏
  • 举报
回复
T Pop()
{
return LinkedList<T>::DelFirst();
}
-->T DelFirst()
{
if(!IsEmpty())
{
Node<T>* node = _head;
T result = node->Record;
if(_head == _rear)
{
_head = NULL;
_rear = NULL;
delete node;
return result;
}
else
{
_head = _head->Next;
delete node;
return result;
}
}
else
{
T result;
return result;
}
}
-->有临时对象产生,所以拷贝构造函数参数是const &才能绑定临时对象的。
所以虽然c++标准规定:A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6)
个人认为拷贝构造函数最好是const X&形式的。原因就是lz这个例子
hbxtght 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 demon__hunter 的回复:]

C/C++ code
LinkedQueue(const LinkedQueue& q)//加个const
{
this->_head = NULL;
this->_rear = NULL;
Node<T>* node = q._head;
while(node != NULL)
{
……
[/Quote]
高人,解释一下
机智的呆呆 2011-11-07
  • 打赏
  • 举报
回复
 LinkedQueue(const LinkedQueue& q)//加个const
{
this->_head = NULL;
this->_rear = NULL;
Node<T>* node = q._head;
while(node != NULL)
{
EnQue(node->Record);
node = node -> Next;
}
}
hbxtght 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 demon__hunter 的回复:]

用的啥编译器 vc6?
[/Quote]
gcc
机智的呆呆 2011-11-07
  • 打赏
  • 举报
回复
用的啥编译器 vc6?
hbxtght 2011-11-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 demon__hunter 的回复:]

提示什么错误
[/Quote]
|276|error: no matching function for call to ‘LinkedQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::LinkedQueue(LinkedQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >)’|
机智的呆呆 2011-11-06
  • 打赏
  • 举报
回复
提示什么错误

64,639

社区成员

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

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