首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

模板种中再定义模板函数的疑问~

2013-07-16 
模板类中再定义模板函数的疑问~~~~~~~~代码如下:#include iostream#include memorytemplate class Ty

模板类中再定义模板函数的疑问~~~~~~~~
代码如下:



#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;

}


假如我再类外定义模板函数的话,必须要写成template<class Type>
template<int size> Vec<Type>& Vec<Type>::operator=(const Type (&Arr)[size])这种形式,

而不能写成template<class Type, int size> Vec<Type>& Vec<Type>::operator=(const Type (&Arr)[size])这种形式.

为什么模板定义必须才拆开写,有什么具体理由吗?

如果这样做,会有以下编译信息:


>------ 已启动生成: 项目: 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 个警告




按编译信息来看,现有声明和定义是一样的,可就是不能通过. 类 调试 Visual?Studio?2008
[解决办法]
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;     
}

热点排行