免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5792 | 回复: 18
打印 上一主题 下一主题

C++中模板分离问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-05 00:25 |只看该作者 |倒序浏览
在一本C++模板书中看到,如果在a.h文件里定义了一个类,实现类中的函数最好也写在a.h里,因为如果写在另一个文件里会让编译器找不到特定的函数声明。
但我将其中的函数定义在a.cpp里,照样可以运行, 是gcc的优化吗?
一起很纠结,如果像书上的那种写好几个文件还特化的方法太麻烦了

论坛徽章:
0
2 [报告]
发表于 2011-05-05 01:56 |只看该作者
模板分离只是一个传说

论坛徽章:
0
3 [报告]
发表于 2011-05-05 08:00 |只看该作者
假如是函数,h中只写声明, 但是假如是模板,那么必须把所有实现写在h中,否则编译器无法在编译时根据模板完成函数或者类的构建。因为编译总是以cpp文件为单位的。

论坛徽章:
0
4 [报告]
发表于 2011-05-06 00:04 |只看该作者
回复 3# benbrick


    但是在g++里将实现写到相应有cpp里可以正常使用,这是怎么回事?

论坛徽章:
0
5 [报告]
发表于 2011-05-06 00:17 |只看该作者
以前的编译器不支持这种模板分离编译

论坛徽章:
0
6 [报告]
发表于 2011-05-06 02:09 |只看该作者
回复  benbrick


    但是在g++里将实现写到相应有cpp里可以正常使用,这是怎么回事?
kingoftime3 发表于 2011-05-06 00:04



    不可能!贴代码出来

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
7 [报告]
发表于 2011-05-06 13:36 |只看该作者
不可能!贴代码出来
changsha 发表于 2011-05-06 02:09



    嗯嗯嗯嗯嗯嗯嗯呃...

论坛徽章:
0
8 [报告]
发表于 2011-05-08 13:51 |只看该作者
回复 6# changsha


    matrix.h

  1. #ifndef MATRIX_H
  2. #define MATRIX_H
  3. #include <iostream>

  4. using namespace std;

  5. template<typename T>
  6. class Matrix
  7. {
  8. private:
  9.         class Matrix1D;
  10. public:
  11.         int rows,cols;
  12.         T *data;
  13.         Matrix(int _rows, int _cols, T *data);
  14.         ~Matrix();
  15.         Matrix operator+(const Matrix &mat);
  16.         Matrix operator*(const Matrix &mat);
  17.         bool operator==(const Matrix &mat);
  18.         Matrix1D operator[](int row){Matrix1D m1d(row, cols, data);return m1d;}
  19.         operator T();
  20. };

  21. template<typename T>
  22. ostream& operator<<(ostream& os, const Matrix<T> &mat);

  23. template<typename T>
  24. istream& operator>>(istream& is, Matrix<T> &mat);


  25. template<typename T>
  26. class Matrix<T>::Matrix1D
  27. {
  28. private:
  29.         int inrow, cols;
  30.         T *indata;
  31. public:
  32.         Matrix1D(int _row, int _cols, T *_data):inrow(_row), cols(_cols),indata(_data){}
  33.         T operator[](int col){return indata[inrow*cols+col];}
  34. };

  35. template<typename T>
  36. class Vector3:public Matrix<T>
  37. {
  38. public:
  39.         Vector3(T t1, T t2, T t3):Matrix<T>(3, 1, NULL){
  40.                 this->data[0]=t1;this->data[1]=t2;this->data[2]=t3;}
  41.         T operator[](int index){return this->data[index];}
  42. };
  43. #endif
复制代码
matrix.cpp

  1. #include "matrix.h"
  2. #include <string.h>
  3. #include <fstream>
  4. #ifdef DEBUG
  5. #include <assert.h>
  6. #endif

  7. template<typename T>
  8. Matrix<T>::Matrix(int _rows, int _cols, T *_data)
  9. {
  10.         rows=_rows,cols=_cols;
  11.         data = new T[rows*cols];
  12.         if (_data == NULL)
  13.         {
  14.                 bzero(data, rows*cols);
  15.         }
  16.         else
  17.         {
  18.                 for(int i=0; i < rows*cols; i++)
  19.                 {
  20.                         data[i] = _data[i];
  21.                 }
  22.         }
  23. }

  24. template<typename T>
  25. Matrix<T>::~Matrix()
  26. {
  27.         //std::cout << "~Matrix" << std::endl;
  28.         if(data != NULL)
  29.         {
  30.                 delete[] data;
  31.         }
  32. }

  33. template<typename T>
  34. Matrix<T> Matrix<T>::operator+(const Matrix<T> &mat)
  35. {
  36. #ifdef DEBUG
  37.         assert(this->rows==mat.rows && this->cols==mat.cols);
  38. #endif
  39.         Matrix<T> result(rows, cols, NULL);
  40.         for(int i=0; i < rows*cols; i++)
  41.         {
  42.                 result.data[i] = this->data[i]+mat.data[i];
  43.         }
  44.         return result;
  45. }

  46. template<typename T>
  47. Matrix<T> Matrix<T>::operator*(const Matrix<T> &mat)
  48. {
  49. #ifdef DEBUG
  50.         assert(cols==mat.rows);
  51. #endif
  52.         Matrix<T> result(rows, mat.cols, NULL);
  53.         for(int i=0; i < result.rows; i++)
  54.         {
  55.                 for(int j=0; j < result.cols; j++)
  56.                 {
  57.                         for(int k=0; k < cols; k++)
  58.                         {
  59.                                 result.data[i*result.cols+j] += this->data[i*cols+k]*mat.data[k*mat.cols+j];
  60.                         }
  61.                 }
  62.         }
  63.         return result;
  64. }

  65. template<typename T>
  66. ostream& operator<<(ostream& os, const Matrix<T> &mat)
  67. {
  68. #ifdef DEBUG
  69.         assert(mat.rows!=0&&mat.cols!=0);
  70. #endif
  71.         os << mat.rows << "\t" << mat.cols << endl;
  72.         for(int i=0; i < mat.rows; i++)
  73.         {
  74.                 for(int j=0; j < mat.cols; j++)
  75.                 {
  76.                         os << mat.data[i*mat.cols+j] << "\t";
  77.                 }
  78.                 os << endl;
  79.         }
  80.         return os;
  81. }

  82. template<typename T>
  83. istream& operator>>(istream& is, Matrix<T> &mat)
  84. {
  85.         if(mat.data != NULL)
  86.                 delete[] mat.data;
  87.         is >> mat.rows >> mat.cols;
  88.         mat.data = new T[mat.rows*mat.cols];
  89.         for(int i = 0; i < mat.rows; i++)
  90.         {
  91.                 for(int j = 0; j < mat.cols; j++)
  92.                 {
  93.                         is >> mat.data[i*mat.cols+j];
  94.                 }
  95.         }
  96.         return is;
  97. }

  98. template<typename T>
  99. Matrix<T>::operator T()
  100. {
  101. #ifdef DEBUG
  102.         assert(rows==1&&cols==1);
  103. #endif
  104.         return data[0];
  105. }

  106. template<typename T>
  107. bool Matrix<T>::operator==(const Matrix &mat)
  108. {
  109.         if(this->rows==mat.rows&&this->cols==mat.cols)
  110.         {
  111.                 for(int i = 0; i < this->rows*this->cols; i++)
  112.                 {
  113.                         if(this->data[i] != mat.data[i])
  114.                                 return false;
  115.                 }
  116.                 return true;
  117.         }
  118.         return false;
  119. }
复制代码

论坛徽章:
0
9 [报告]
发表于 2011-05-08 14:08 |只看该作者
回复  changsha


    matrix.hmatrix.cpp
kingoftime3 发表于 2011-05-08 13:51



    你只是定义了模板类但是从来没有实例化,当然没有问题

论坛徽章:
0
10 [报告]
发表于 2011-05-08 14:18 |只看该作者
回复 9# changsha


    这样用也没有问题
  1. int main()
  2. {
  3.                 double d[] = {1,2,3,4};
  4.                 Matrix<double> m(2,2,d);
  5.                 cout << m << endl;
  6.                 return 0;
  7. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP