| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1962 人关注过本帖
标题:[请教]关于模板类的实例化问题
只看楼主 加入收藏
caoaihua3
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-4-20
收藏
 问题点数:0 回复次数:9 
[请教]关于模板类的实例化问题
经过上一次请教,现在重新定义一模板类,运行编译都通过.测试问件下通过如下 :
int main(){
    int col=5,row=6;
    arrays<int>mat1(row,col);
    arrays<int>mat2(row,col);
    for (int i=0;i<row;i++){
        for (int j=0;j<col;j++){
            mat1(i,j)=i*j;
            mat2(i,j)=i+j;
        }
    }
    mat1.disp();
    mat2.disp();
  }
但是现在我要把测试文件改写成如下形式:
int main(){
    int col=5,row=6;
    arrays<int>mat1(row,col);
    arrays<double>mat2(row,col);
    for (int i=0;i<row;i++){
        for (int j=0;j<col;j++){
            mat1(i,j)=i*j;
            mat2(i,j)=i+j;
        }
    }
    mat1.disp();
    mat2.disp();
   
   
   
   }
其是就是两个实例化函数,一个实例化为int,另一个实例化double,这里就会有编译问题
38 D:\Program Files\DEV-CPP\WORK\matrix\arrays.h new declaration `arrays<double> zeros(int, int)'
38 D:\Program Files\DEV-CPP\WORK\matrix\arrays.h ambiguates old declaration `arrays<int> zeros(int, int)'
再请问出现上述情况的可能问题有哪些,难道在一个main里面不能实例化不同类型的类???
同样,将它们都改为double型,又可以通过运行
再次请教各位
搜索更多相关主题的帖子: 模板类 int col row 实例 
2008-03-23 10:11
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
你把你声明放double的模板里面放的元素改成double

学习需要安静。。海盗要重新来过。。
2008-03-23 10:16
caoaihua3
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-4-20
收藏
得分:0 
什么意思啊.听不太懂,模板里面int或者double都是一样的...
2008-03-23 10:29
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
你把你模板给出来看看。。。

学习需要安静。。海盗要重新来过。。
2008-03-23 10:31
caoaihua3
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-4-20
收藏
得分:0 
template <typename T>
class arrays{
      private:
              int col,row;
              vector<vector<T> > mat;
      public:
             arrays(){};
             arrays<T>(int i,int j){
                           row=i;
                           col=j;
                           mat.resize(row);
                           for(int i=0;i<row;i++){
                                   mat[i].resize(col);
                           }
             }
 }
原来 是这样的,有什么问题吗?
2008-03-23 10:31
caoaihua3
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-4-20
收藏
得分:0 
这只是构造函数,还有一些都是在这个基础上的
2008-03-23 10:35
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
#include<iostream>
#include<cstdlib>
#include<vector>
#include<iomanip>
using namespace std;
namespace a{
    template <typename T>
class arrays{
      private:
              int col,row;
              vector<vector<T> > mat;
      public:
             arrays(){};
             arrays<T>(int i,int j){
                           row=i;
                           col=j;
                           mat.resize(row);
                           for( i=0;i<row;i++){
                                   mat[i].resize(col);
                           }
             }
             T& operator()(size_t i,size_t j){
                    if (i<row&&j<col){
                                      return (mat.at(i).at(j));}
                    else{
                         std::cout<<"the  cite write function wrong"<<endl; }
             }
             const T operator()(size_t i,size_t j) const{
                    if (i<row&&j<col){
                                      return (mat.at(i).at(j));}
                    else{
                         std::cout<<"the  cite read function wrong"<<endl; }
             }
             friend arrays<T> zeros(int m,int n,T t){
                      arrays<T> temp(m,n);
                      for(int i=0;i<m;i++){
                              for(int j=0;j<n;j++){
                                      temp(i,j)=0;
                              }
                      }
                      temp.row=m;temp.col=n;
                      return temp;
             }
             friend arrays<T> ones(int m,int n,T t){
                      arrays<T> temp(m,n);
                      for(int i=0;i<m;i++){
                              for(int j=0;j<n;j++){
                                      temp(i,j)=1;
                              }
                      }
                      temp.row=m;temp.col=n;
                      return temp;
             }
             friend arrays<T> eyes(int m,T t){
                      arrays<T> temp(m,m);
                      for(int i=0;i<m;i++){
                              for(int j=0;j<m;j++){
                                      if (i==j){
                                                temp(i,j)=1;}
                                      else
                                      temp(i,j)=0;
                              }
                      }
                      temp.row=m;temp.col=m;
                      return temp;
             }
             friend arrays<T>rands(int m,int n,T t){
                    arrays<T> temp(m,n);
                    srand((unsigned)time(NULL));
                    for(int i=0;i<m;i++){
                            for(int j=0;j<n;j++){
                                    temp(i,j)=(T)(rand()/RAND_MAX);
                            }
                    }
                    return temp;
             }
                                    
             void disp(){
                  for(int i=0;i<row;i++){
                          for(int j=0;j<col;j++){
                              cout<<std::setw(7)<<mat.at(i).at(j);
                          }
                          std::cout<<endl;
                  }
                  std::cout<<endl;
             }
             int get_row(){
                 return(row);}
             int get_col(){
                 return(col);}
             friend arrays<T> get_i_row(arrays<T>& temp1,int i){
                    int row1,col1;
                    row1=temp1.get_row();col1=temp1.get_col();
                    if (i<row1){
                               int row_temp=1;
                               arrays<T> temp(row_temp,col1);
                               for(int j=0;j<col1;j++){
                                       temp(0,j)=temp1(i,j);
                               }
                    return temp;
                    }
                    else
                        std::cout<<"the index beyound the row rage"<<endl;
             }
             friend arrays<T> get_i_col(arrays<T>& temp1,int i){
                    int row1,col1;
                    row1=temp1.get_row();col1=temp1.get_col();
                    if (i<col1){
                               int row_temp=1;
                               arrays<T> temp(row1,row_temp);
                               for(int j=0;j<row1;j++){
                                       temp(j,0)=temp1(j,i);
                               }
                    return temp;
                    }
                    else
                        std::cout<<"the index beyound the col rage"<<endl;
                     }                          
             friend arrays<T> operator +(arrays<T>& temp1,arrays<T>& temp2){
                    int row1,row2,col1,col2;
                    row1=temp1.get_row();col1=temp1.get_col();
                    row2=temp2.get_row();col2=temp2.get_col();
                    if (row1==row2&&col1==col2){
                              arrays<T> temp(row1,col1);
                              for(int i=0;i<row1;i++){
                                       for(int j=0;j<col1;j++){
                                              temp(i,j)=temp1(i,j)+temp2(i,j);
                                       }
                              }
                              temp.row=row1;temp.col=col1;
                              return(temp);
                    }
                    
                    else{
                        std::cout<<"the add functon is wrong "<<endl;
                    }
             }
             friend arrays<T> operator +(arrays<T>& temp1,T temp2){
                    int row1,col1;
                    row1=temp1.get_row();col1=temp1.get_col();
                    arrays<T> temp(row1,col1);
                    for(int i=0;i<row1;i++){
                            for(int j=0;j<col1;j++){
                                    temp(i,j)=temp1(i,j)+temp2;
                            }
                    }
                    temp.row=row1;temp.col=col1;
                    return(temp);
             }
             friend arrays<T> operator -(arrays<T>& temp1,T temp2){
                    int row1,col1;
                    row1=temp1.get_row();col1=temp1.get_col();
                    arrays<T> temp(row1,col1);
                    for(int i=0;i<row1;i++){
                            for(int j=0;j<col1;j++){
                                    temp(i,j)=temp1(i,j)-temp2;
                            }
                    }
                    temp.row=row1;temp.col=col1;
                    return(temp);
             }
             friend arrays<T> operator *(arrays<T>& temp1,T temp2){
                    int row1,col1;
                    row1=temp1.get_row();col1=temp1.get_col();
                    arrays<T> temp(row1,col1);
                    for(int i=0;i<row1;i++){
                            for(int j=0;j<col1;j++){
                                    temp(i,j)=temp1(i,j)*temp2;
                            }
                    }
                    temp.row=row1;temp.col=col1;
                    return(temp);
             }
             friend arrays<T> operator /(arrays<T>& temp1,T temp2){
                    int row1,col1;
                    row1=temp1.get_row();col1=temp1.get_col();
                    arrays<T> temp(row1,col1);
                    for(int i=0;i<row1;i++){
                            for(int j=0;j<col1;j++){
                                    temp(i,j)=temp1(i,j)/temp2;
                            }
                    }
                    temp.row=row1;temp.col=col1;
                    return(temp);
             }      
                    
            
             friend arrays<T> operator -(arrays<T>& temp1,arrays<T>& temp2){
                    int row1,row2,col1,col2;
                    row1=temp1.get_row();col1=temp1.get_col();
                    row2=temp2.get_row();col2=temp2.get_col();
                    if (row1==row2&&col1==col2){
                              arrays<T> temp(row1,col1);
                              for(int i=0;i<row1;i++){
                                       for(int j=0;j<col1;j++){
                                              temp.mat.at(i).at(j)=temp1.mat.at(i).at(j)-temp2.mat.at(i).at(j);
                                       }
                              }
                              temp.row=row1;temp.col=col1;
                              return(temp);
                    }
                    
                    else{
                        std::cout<<"the sub function is wrong "<<endl;
                    }
             }
             friend arrays<T> operator *(arrays<T>& temp1,arrays<T>& temp2){
                    int row1,row2,col1,col2;
                    row1=temp1.get_row();col1=temp1.get_col();
                    row2=temp2.get_row();col2=temp2.get_col();
                    if (col1==row2){
                              arrays<T> temp(row1,col2);
                              for(int i=0;i<row1;i++){
                                       for(int j=0;j<col2;j++){
                                              arrays<T> temp_1=get_i_row(temp1,i);
                                              arrays<T> temp_2=get_i_col(temp2,j);
                                              int sum=0;
                                              for (int sum_i=0;sum_i<col1;sum_i++){
                                                  sum=sum+temp_1(0,sum_i)*temp_2(sum_i,0);
                                              }
                                              temp(i,j)=sum;
                                       }
                              }
                              temp.row=row1;temp.col=col2;
                              return(temp);
                    }
                    
                    else{
                        std::cout<<"the mutil function is wrong "<<endl;
                    }
             }
             arrays<T> operator=(arrays<T>& temp1){
                       arrays<T> temp;
                       temp.row=temp1.row;
                       temp.col=temp1.col;
                       for(int i=0;i<temp1.row;i++){
                                       for(int j=0;j<temp1.col;j++){
                                               temp.mat.at(i).at(j)=temp1.mat.at(i).at(j);
                                       }
                       }
                       return(temp);
             }              
             friend arrays<T> copy(arrays<T>& temp1  ){
                      int rows,cols;
                      rows=temp1.get_row();cols=temp1.get_col();
                      arrays<T> temp2(rows,cols);
                      for(int i=0;i<rows;i++){
                              for(int j=0;j<cols;j++){
                                      temp2(i,j)=temp1(i,j);
                              }
                      }
                      temp2.row=rows;temp2.col=cols;
                      return(temp2);
             }
                                             
                    
};

}
int main(){
    int col=5,row=6;
    a::arrays<int>mat1(row,col);
   // int rows=5,cols=6;
    a::arrays<double>mat2(row,col);
    for (int i=0;i<row;i++){
        for (int j=0;j<col;j++){
            mat1(i,j)=i*j;
            mat2(i,j)=i+j;
        }
    }
    mat1.disp();
    mat2.disp();
   
   
   
    system("pause");
    return 1;
}

学习需要安静。。海盗要重新来过。。
2008-03-23 12:41
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
其实也就是函数返回值不能作为函数重载的依据。。。为了避免空间污染可以自己做一个命名空间。。。再者里面有几个出口有点小问题。。。不过你很强了。。。

学习需要安静。。海盗要重新来过。。
2008-03-23 12:55
caoaihua3
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-4-20
收藏
得分:0 
好的,谢谢啊,下次有问题还要请你帮忙
2008-03-23 13:06
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
不客气。。一起进步。。。

学习需要安静。。海盗要重新来过。。
2008-03-23 13:06
快速回复:[请教]关于模板类的实例化问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040260 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved