类模板采用包含编译模型编译报错
按照c++ primer 第四版中所述的包含编译模型实例,在vs2010中编译怎么报错呢?代码如下
- C/C++ code
//MyQueue.h 头文件#ifndef MYQUEUE_H#define MYQUEUE_H#include "stdafx.h"template <class T> class MyQueue { public: T GetMember(); MyQueue(const T& param); private: T FirstMember; };#include "MyQueue.cpp" //包含模板定义的源文件#endif//模板定义源文件#include "stdafx.h" template<class T> MyQueue<T>::MyQueue(const T& param) { FirstMember=param; }template <class T>T MyQueue<T>::GetMember(){ return FirstMember;}
错误描述如下:
错误1 error C2143: 语法错误 : 缺少“;”(在“<”的前面) myqueue.cpp4行
错误2 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int myqueue.cpp4行
错误3 error C2988: 不可识别的模板声明/定义 myqueue.cpp4行
错误4 error C2059: 语法错误:“<” myqueue.cpp4行
错误5 error C2039: “GetMember”: 不是“`global namespace'”的成员myqueue.cpp9行
错误6 error C2143: 语法错误 : 缺少“;”(在“{”的前面) myqueue.cpp10行
错误7 error C2447: “{”: 缺少函数标题(是否是老式的形式表?) myqueue.cpp10行
怎么出了这么多错误呢?
(函数模板用包含编译模型编译就没问题)
[解决办法]
您好,模板目前还不支持头文件和源文件的分离,要都写到.h文件中。