您的位置:首页 > 其它

使用包含编译模型编译模板类时出现无法理解的错误

2011-08-08 20:51 549 查看
错误代码:

文件1:LinkedList.h

//LinkedList.h
template<typename T>
class LinkedList
{
public:
LinkedList();
~LinkedList();
void clearall();
private:
int*begin;
int listcounter;
};

#include "LinkedList.cpp"

文件2:LinkedList.cpp

//LinkedList.cpp

template <typename T>
LinkedList<T>::~LinkedList()
{
clearall();
}

template<typename T>
LinkedList<T>::LinkedList()
{
begin=0;
listcounter=0;
}
template<typename T>
void LinkedList<T>::clearall()
{
}


根据C++primer第四版关于包含编译模型的描述(543页),将函数模板的定义与声明分在两个文件中,但该测试编译时出现了难以理解的错误:

错误 1 error C2143: 语法错误 : 缺少“;”(在“<”的前面)
e:\文档\documents\visual studio 2008\projects\test\test\linkedlist.cpp
5 test

错误 2 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
e:\文档\documents\visual studio 2008\projects\test\test\linkedlist.cpp
5 test

错误 3 error C2988: 不可识别的模板声明/定义
e:\文档\documents\visual studio 2008\projects\test\test\linkedlist.cpp
5 test

错误 4 error C2059: 语法错误 : “<”
e:\文档\documents\visual studio 2008\projects\test\test\linkedlist.cpp
5 test

错误 5 error C2588: “::~LinkedList”: 非法的全局 析构函数
e:\文档\documents\visual studio 2008\projects\test\test\linkedlist.cpp
5 test

错误 6 fatal error C1903: 无法从以前的错误中恢复;正在停止编译
e:\文档\documents\visual studio 2008\projects\test\test\linkedlist.cpp
5 test

改正之后的代码:

//linkedlistitem.h
#ifndef LINKEDLISTITEM_H
#define LINKEDLISTITEM_H

namespace LinkedList
{
template<class T>
class linkedlist;

template<class T>
class linkedlistitem
{
friend class linkedlist<T>;
protected:
T item;
linkedlistitem* preview;
linkedlistitem* next;
public:
linkedlistitem();
linkedlistitem(const T& itemin,linkedlistitem*p=0,linkedlistitem*n=0);
bool operator ==(const linkedlistitem<T>&)const;
bool operator <(const linkedlistitem<T>&)const;
bool operator >(const linkedlistitem<T>&)const;
bool operator <=(const linkedlistitem<T>&)const;
bool operator >=(const linkedlistitem<T>&)const;
bool operator !=(const linkedlistitem<T>&)const;
void changeitem(const T&);
void changethis(linkedlistitem*);
//	void changethis(linkedlistitem&);

linkedlistitem*getpreview(){return preview;}
linkedlistitem*getnext(){return next;}
};

}
#include "linkedlistitem.cpp"
#endif
#ifndef LINKEDLISTITEM_CPP
#define LINKEDLISTITEM_CPP

#include <assert.h>
#include "linkedlistitem.h"
namespace LinkedList
{
//template<class T>
//class linkedlistitem;

template<typename T>
linkedlistitem<T>::linkedlistitem()
{
T* tmp=new T();
item=*tmp;
delete tmp;
next=0;
preview=0;
}
template<typename T>
linkedlistitem<T>::linkedlistitem(const T& itemin,linkedlistitem*p=0,linkedlistitem*n=0):item(itemin),preview(p),next(n){ }

template<typename T>
bool linkedlistitem<T>::operator <(const LinkedList::linkedlistitem<T> & i) const
{
return item<i.item;
}

template<typename T>
bool linkedlistitem<T>::operator <=(const LinkedList::linkedlistitem<T>&i)const
{
return item<=i.item;
}

template<typename T>
bool  linkedlistitem<T>::operator >(const linkedlistitem<T>&i)const
{
return item>i.item;
}

template<typename T>
bool  linkedlistitem<T>::operator >=(const linkedlistitem<T>&i)const
{
return item>=i.item;
}

template<typename T>
bool  linkedlistitem<T>::operator ==(const linkedlistitem<T>&i)const
{
return item==i.item;
}
template<typename T>
bool  linkedlistitem<T>::operator !=(const linkedlistitem<T>&i)const
{
return item!=i.item;
}

template<typename T>
void linkedlistitem<T>::changeitem(const T& i)
{
this->item=i;
}
template<typename T>
void linkedlistitem<T>::changethis(linkedlistitem<T>* i)
{
if(this->preview)
{
this->preview->next=i;
i->preview=this->preview;
i->next=this;
this->preview=i;
}
else
{
//如果是第一个
this->preview=i;
i->preview=0;
i->next=this;
}
}
//template<typename T>
//void linkedlistitem<T>::changethis(linkedlistitem<T>&i)
//{
//	//防止指向栈区已被释放的内存
//	linkedlistitem<T>*tmp=new linkedlistitem<T>(i.item,i.preview,i.next);
//	changethis(tmp);
//	/////////////////////////////////
//assert(tmp->getnext());
//assert(getpreview());
//}
}
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐