模板类中再定义模板函数的疑问~~~~~~~~
代码如下:
#include <iostream>
#include <memory>
template <class Type>
class Vec
{
public:
Vec()
{
data = avail = alloc.allocate(100);
}
~Vec()
{
alloc.deallocate(data, 100);
}
typedef Type* iterator;
iterator& begin(){ return data;}
iterator& end() { return avail;}
void push_back(const Type& val){ alloc.construct(avail ++, val);}
template<int size> Vec& operator=(const Type (&Arr)[size])
{
int i = 0;
while (i != size )
{
push_back(*(Arr + i));
i++;
}
return *this;
}
private:
iterator data;
iterator avail;
std::allocator<Type> alloc;
};
/*
template<class Type>
template<int size> Vec<Type>& Vec<Type>::operator=(const Type (&Arr)[size])
{
int i = 0;
while(i != size )
{
push_back(*(Arr + i));
i ++;
}
return *this;
}
*/
using namespace std;
int main()
{
Vec<int> test;
int a[] = {0, 1, 2, 3, 4};
test = a;//数组元素赋值给容器
for (Vec<int>::iterator num_iter = test.begin(); num_iter != test.end(); num_iter ++)
{
cout << *num_iter << endl;
}
return 0;
}
>------ 已启动生成: 项目: test, 配置: Debug Win32 ------
1>正在编译...
1>test.cpp
1>d:\users\acer\documents\visual studio 2008\projects\test1\test\test.cpp(201) : error C2244: “Vec<Type>::operator =”: 无法将函数定义与现有的声明匹配
1> 定义
1> 'Vec<Type> &Vec<Type>::operator =(const Type (&)[size])'
1> 现有声明
1> 'Vec<Type> &Vec<Type>::operator =(const Type (&)[size])'
1>生成日志保存在“file://d:\Users\acer\Documents\Visual Studio 2008\Projects\test1\test\Debug\BuildLog.htm”
1>test - 1 个错误,0 个警告
template <class Type>
class Vec
{
public:
template<class Type2> Vec& test2(Type2);
};
template<class Type>
template<class Type2> Vec<Type>& Vec<Type>::test(Type2 tt)
{
return *this;
}
//按照上面的写法
//当调用test2函数时Type是已知类型,Type2根据参数确定
//调用成员函数的时候根据参数确定Type2或者手工指定Type2
//而此处的 template<class Type> 应该是声明Type为一个类型而已,不能更改
//如
// Vec<int> vc;
// vc.test('a');
// vc.test<int> ('a');
//如果同时写在一起如下
template<class Type, class Type2> Vec<Type>& Vec<Type>::test(Type2 tt)
{
return *this;
}
//则表示在调用test函数的时候再确定Type和Type2这两个类型,但是此时Type却是已经确定了
//但是根据语法来说 vc.test<double, int>('a') 这样的写法没问题,但是却更改了Type的类型
//还会引起定义不明确的问题
//所以才不行的吧
//类外,大概要这样定义吧
//1)
template<class Type,int size>
Vec<Type>& Vec<Type>::operator= (const Type (&Arr)[size])
{
int i = 0;
while(i != size )
{
push_back(*(Arr + i));
i ++;
}
return *this;
}
//2)
template<class Type,int size>
Vec<Type>& Vec<Type>::operator=<> (const Type (&Arr)[size])
{
int i = 0;
while(i != size )
{
push_back(*(Arr + i));
i ++;
}
return *this;
}
//3)
template<class Type,int size>
Vec<Type>& Vec<Type>::operator=<Type,size> (const Type (&Arr)[size])
{
int i = 0;
while(i != size )
{
push_back(*(Arr + i));
i ++;
}
return *this;
}
using namespace std;
int main()
{
Vec<int> testInt,testdouble;
int a[] = {0, 1, 2, 3, 4};
double b[]={1.1,2.2,3.3,4.4}
testInt = a;//数组元素赋值给容器
testdouble =b;
for (Vec<int>::iterator num_iter = testInt.begin(); num_iter != testInt.end(); num_iter ++)
{
cout << *num_iter << endl;
}
for (Vec<int>::iterator num_iter = testDouble.begin(); num_iter != testDouble.end(); num_iter ++)
{
cout << *num_iter << endl;
}
return 0;
}