使用包含编译模型编译模板类时出现无法理解的错误
2011-08-08 20:51
549 查看
错误代码:
文件1:LinkedList.h
根据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
改正之后的代码:
文件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
相关文章推荐
- C++中模板类使用友元模板函数 和友员非模版函数!使用不当出现编译错误:无法解析的外部符号…
- 引用原因.NET下编译C++代码时出现fatal error C1083: 无法打开包含文件:“iostream.h”: No such file or directory错误的解决方法.
- WCF编程]WCF使用Net.tcp绑定时候出现错误:元数据包含无法解析的引用
- WCF使用Net.tcp绑定时候出现错误:元数据包含无法解析的引用
- .NET下编译C++代码时出现fatal error C1083: 无法打开包含文件:“iostream.h”: No such file or directory错误的解决方法.
- 使用Web Deployment部署的项目上传后出现编译错误
- 关于CMake生成包含PCL库和CGAL库的工程时出现“无法解析的外部符号”的错误
- 使用winsock2.h可能出现的两种编译错误
- MSys中使用configure出现无法找到pr错误解决方案
- vc++ 使用64位平台编译,运行程序出现0xc000007b错误的解决方案
- WPF编译时出现【程序“\MVC_DHCC.exe”不包含适合于入口点的静态“Main”方法】的错误
- eclipse编译出现错误: 找不到或无法加载主类的解决
- VC中使用wxWidgets 出现的编译错误 error C2059: syntax error : 'constant' 解决
- error 25015安装程序集“C:\Windows\Microsoft .NET\Framework\v2.0.50727\xxx.dll”失败,原因是出现了系统错误:另一个程序正在使用此文件,进程无法访问
- 使用javah生成.h文件, 出现无法访问android.app,Activity的错误的解决
- 【mfc】vc++ 使用64位平台编译,运行程序出现0xc000007b错误的解决方案
- 使用px4编译代码时出现“no module names site”错误的解决办法
- IIS出现错误“无法使用已在使用中的标识启动新的会话”
- (c#)AutoCAD二次开发,netload后,重新调试编译出现“XXXXXX正由另一进程使用,因此无法访问文件”
- StreamWriter出现下面错误“正由另一进程使用,因此该进程无法访问该文件”