在线等关于template的问题,请各位不吝赐教如下代码:C/C++ code#include iostreamtemplatetypename T c
在线等关于template的问题,请各位不吝赐教
如下代码:
- C/C++ code
#include <iostream>template<typename T> class A{public: A():point_ptr(creat_point){} ~A(){delete point_ptr;}private: template<typename Type> struct Point; Point<T> *point_ptr; Point<T> * creat_point();};template<typename T> template<typename Type> struct A<T>::Point { Type x,y;};template<typename T> typename A<T>::template Point<T> *A<T>::creat_point(){ return new Point<T>();}int main(){ return 0;}在gcc中编译通过,在vs2010下编译提示:unable to match function definition to an existing declaration
请各位不理赐教,多多指教
[解决办法]
这个错误意识是: struct Point怎么办的?无法找到定义啊
[解决办法]
各家编译器肯定有自己的特点,不要强求,就好像那些一连串++i和i++的运算结果一样的,编译器不同,结果就可能不同,标准又没有规定你这个必须像GCC那样编译。
[解决办法]
template<typename T>
class A
{
public:
A():point_ptr(creat_point()){}
~A(){delete point_ptr;}
private:
template<typename Type>
struct Point
{
Type x, y;
};
Point<T>* point_ptr;
Point<T>* creat_point()
{
return new Point<T>();
}
};
vs2005
[解决办法]
给这模版语法跪了,估计是VC的bug
另外模版函数真没分开写的意义
[解决办法]
