C++中模板分离问题

发布于 2022-10-15 07:53:45 字数 153 浏览 27 评论 0

在一本C++模板书中看到,如果在a.h文件里定义了一个类,实现类中的函数最好也写在a.h里,因为如果写在另一个文件里会让编译器找不到特定的函数声明。
但我将其中的函数定义在a.cpp里,照样可以运行, 是gcc的优化吗?
一起很纠结,如果像书上的那种写好几个文件还特化的方法太麻烦了

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

蔚蓝源自深海 2022-10-22 07:53:45

模板分离只是一个传说

时常饿 2022-10-22 07:53:45

假如是函数,h中只写声明, 但是假如是模板,那么必须把所有实现写在h中,否则编译器无法在编译时根据模板完成函数或者类的构建。因为编译总是以cpp文件为单位的。

这样的小城市 2022-10-22 07:53:45

回复 3# benbrick

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

花之痕靓丽 2022-10-22 07:53:45

以前的编译器不支持这种模板分离编译

以往的大感动 2022-10-22 07:53:45

回复  benbrick

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

    不可能!贴代码出来

蹲在坟头点根烟 2022-10-22 07:53:45

不可能!贴代码出来
changsha 发表于 2011-05-06 02:09

    嗯嗯嗯嗯嗯嗯嗯呃...

瑾兮 2022-10-22 07:53:45

回复 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. }

复制代码

∝单色的世界 2022-10-22 07:53:45

回复  changsha

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

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

静谧幽蓝 2022-10-22 07:53:45

回复 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. }

复制代码

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文