VS2013 编纂-类模板-代码 -编译出错
VS2013 编写-类模板-代码 -编译出错
LinkList.h如下:
main.cpp如下:
编译错误提示有:(全在main.cpp)
error C2062:意外的类型int 第8行
error C2065 未声明的标示符 cout endl LinkList m_list
error C2228 .Insert左边必须有类/结构/集合
类模板的定义和实现已经放在了同一个文件(必须是.cpp吗?)中,为何还是会有上述错误呢?
------解决思路----------------------
friend ostream & operator <<(ostream &out, LinkList<type> &L);
---->
friend ostream& operator<< <type> (ostream &out, LinkList<type>& L);
LinkList.h如下:
#include <iostream>
#include <assert.h>
#include <stdafx.h>
using namespace std;
template <class type>
class ListNode{
public:
ListNode(){next = NULL;}
ListNode(const type & item, ListNode<type> *next1 = NULL){
data = item;
next = next1;
}
type data;
ListNode<type> * next;
};
template <class type>
class LinkList{
public:
LinkList(){
head = new ListNode<type>;
length = 0;
}
LinkList(LinkList<type> & L){
Copy(1);
}
~LinkList(){
MakeEmpty();
delete head;
}
ListNode<type> * GetHead() {return head;} //获得表头节点指针
ListNode<type> * GetNext(ListNode<type> & n) {return n.next == head?n.next->next:n.next;} //获得节点n的下一个节点位置
type GetData(int i); //取出链表中的第i个元素
bool SetData(type value, int i); //将链表中的第i个元素赋值为value
ListNode<type> * FindByVal(type value); //找到值为value的节点指针
ListNode<type> * FindByPos(int pos); //找到链表中第pos个节点指针
void MakeEmpty(); //清空整个链表
bool Insert(type value, int i); //在第i个位置插入值为value的节点
type RemoveByPos(int pos); //删除掉位置pos处的节点
type RemoveByVal(type value); //删除掉值为value的节点
LinkList<type> & Copy(LinkList<type> &L); //拷贝函数
LinkList<type> & operator = (LinkList<type> &L); //重载赋值运算符,同类型链表赋值
friend ostream & operator <<(ostream &out, LinkList<type> &L); //重载输出运算符
protected:
ListNode<type> * head;
int length;
};
//类模板中函数的实现如下:
template <class type>
ListNode<type> * LinkList<type>::FindByVal(type value){
ListNode<type> *p = head->next;
int i = 1;
while(i++ <= length && value != p->data)
p = p->next;
return p;
}
template <class type>
ListNode<type> * LinkList<type>::FindByPos(int pos){
if(pos < 0 || pos > length) return NULL;
if(pos == 0) return head;
ListNode<type> *p = head->next;
int i = 1;
while(i++ < pos && p!= NULL)
p = p->next;
return p;
}
template <class type>
type LinkList<type>::GetData(int pos){
ListNode<type> *p = FindByPos(pos);
assert(p && p != head);
return p->data;
}
template <class type>
bool LinkList<type>::SetData(type value, int pos){
ListNode<type> *p = FindByPos(pos);
if(!p || p == head)
return false;
else
p->data = value;
return true;
}
template <class type>
void LinkList<type>::MakeEmpty(){
ListNode<type> *p = head->next;
int i = 1;
while(i++ <= length){
head->next = p->next;
delete p;
p = head->next;
}
length = 0;
}
template <class type>
bool LinkList<type>::Insert(type value, int i){
ListNode<type> *p = FindByPos(i-1);
if(!p) return false;
ListNode<type> *node = new ListNode<type> (value,p->next);
assert(node);
p->next = node;
length++;
return true;
}
template <class type>
type LinkList<type>::RemoveByPos(int pos){
ListNode<type> *p = FindByPos(pos-1), *q;
assert(p && p->next != NULL);
q = p->next;
type val = q->data;
p->next = q->next;
delete q;
length--;
return val;
}
template <class type>
type LinkList<type>::RemoveByVal(type val){
ListNode<type> *p, *q = head;
p = q->next;
while((p->next != NULL) && (p->next->data != val)){
p = p->next;
}
q = p->next;
p->next = q->next;
delete q;
length--;
return val;
}
template <class type>
LinkList<type> & LinkList<type>::Copy(LinkList<type> &L){
if(! L.head)
return *this;
ListNode<type> *p = NULL, *q = NULL, *r = NULL;
this->length = L.length;
head = new ListNode<type>;
assert(head);
head->data = L.head->data;
head->next = NULL;
p = head;
q = L.head->next;
while(q){
r = new ListNode<type>;
if(r == NULL)
return *this;
r->data = q->data;
r->next = NULL;
p->next = r;
q = q->next;
p = p->next;
}
return *this;
}
template <class type>
LinkList<type> & LinkList<type>::operator = (LinkList<type> &L){
if(head)
MakeEmpty();
Copy(L);
return *this;
}
template <class type>
ostream & operator <<(ostream &out, LinkList<type> & L){
ListNode<type> *p = L.head->next;
out<<"length:"<<L.length<<"\ndata:";
while(p){
out<<p->data<<" ";
p = p->next;
}
out<<"\n";
return out;
}
main.cpp如下:
#include <iostream>
#include "LinkList.h"
#include <stdafx.h>
using namespace std;
int main()
{
LinkList< int > m_list;
m_list.Insert(3,1);
m_list.Insert(2,2);
m_list.Insert(4,3);
cout<<m_list<<endl;
return 0;
}
编译错误提示有:(全在main.cpp)
error C2062:意外的类型int 第8行
error C2065 未声明的标示符 cout endl LinkList m_list
error C2228 .Insert左边必须有类/结构/集合
类模板的定义和实现已经放在了同一个文件(必须是.cpp吗?)中,为何还是会有上述错误呢?
------解决思路----------------------
friend ostream & operator <<(ostream &out, LinkList<type> &L);
---->
friend ostream& operator<< <type> (ostream &out, LinkList<type>& L);