【问题标题】:creating multiple matrix with different constructors使用不同的构造函数创建多个矩阵
【发布时间】:2015-01-31 11:59:05
【问题描述】:

我有一个任务,我需要使用类型为 T 元素的矩阵类。我有一个使用 2 个 int 的构造函数、一个复制构造函数、一个使用字符串的构造函数和一个析构函数。矩阵 A 使用第一个构造函数并且工作正常,矩阵 B 使用第二个并且打印出来也很好,但是我对矩阵 C 有问题。如果我自己声明它(从 main 中删除矩阵 a 和 b)它也会打印,但是当一切都在那里,它打印出如下内容: 3735748 3745176 3745176 3735748 (代替 5 6 7 8)

每次编译时数字都会改变。 我觉得这应该很明显,但我是初学者,无法弄清楚..欢迎任何帮助!谢谢

template <class T>
class matrice
{
      private :
              unsigned int nLignes; //number of rows
              unsigned int nColonnes; //nbr of cols
              T** rep;
      public :
              matrice( unsigned int nl, unsigned int nc)
              {
                       setLignes(nl);
                       setColonnes(nc);

                       rep = new T*[nl];

                       for(int i = 0; i < nl; i++){
                               rep[i] = new T[nc];
                       }

                       for(int i = 0; i < nl; i++){
                               for(int j = 0; j < nc; j++){
                                       rep[i][j] = (T) i*j;
                               }
                       }
              }
              matrice( const matrice& mat)// my copy constructor
              {
                       setLignes(mat.nLignes);
                       setColonnes(mat.nColonnes);

                       rep = new T*[nLignes];

                       for(int i = 0; i < nLignes; i++){
                               rep[i] = new T[nColonnes];
                       }

                       for(int i = 0; i < nLignes; i++){
                               for(int j = 0; j < nColonnes; j++){
                                       rep[i][j] = (T) i*j;
                               }
                       }


              }
              matrice( const std::string& UnString)//constructor using a string 
//"nl,nc,val1, val2.."
              {        

                       std::vector<int> vect;

                       std::stringstream ss(UnString);

                       int i;

                       while (ss >> i)
                       {
                             vect.push_back(i);

                             if (ss.peek() == ',')
                             ss.ignore();
                       }
                       setLignes(vect.at(0));
                       setColonnes(vect.at(1));
                       int taille = nLignes * nColonnes;
                       vect.erase(vect.begin(), vect.begin()+2);
                       if(taille == vect.size()){ // making sure enough values to 
//fill array
                                 int n = 0;
                                 for(int i = 0; i < nLignes; i++){
                                 rep[i] = new T[nColonnes];
                                 }

                                 for(int i = 0; i < nLignes; i++){
                                         for(int j = 0; j < nColonnes; j++){
                                                 rep[i][j] = vect.at(n);
                                                 n++;

                                         }
                                 }
                       }


              }


              ~matrice()
              {
                        delete[] rep;
              }

              void afficher();//printing function
              void setLignes(int l)
              {
                   nLignes = l;
              }
              void setColonnes(int c)
              {
                   nColonnes = c;
              }
};

template <class T>
void matrice<T>::afficher()//printing function
{
    int i,j;

    for (i=0;i < nLignes;i++) 
    {
        for(j=0;j < nColonnes;j++)
        {
            cout << rep[i][j] << " ";
        }
        cout << "\n";
    }
}

/*
 *
 *MAIN*
 ******/


int main(int argc, char *argv[])
{

    matrice <unsigned int> mat(5,3);//MATRIX A
    matrice <unsigned int> a = mat;
    a.afficher();
    {
                 matrice <unsigned int> mat(6,6);//MATRIX B
                 matrice <unsigned int> b = mat;
                 b.afficher();
    }


    matrice <unsigned int> c("2,2,5,6,7,8");//MATRIX C !! problem

    c.afficher();




    system("pause");
    return 0;

}

【问题讨论】:

    标签: c++ matrix constructor destructor


    【解决方案1】:

    您忘记在构造函数中使用字符串为 rep 分配空间。

    另外,你的解构器有内存泄露的风险,请自行检查。

    【讨论】:

      猜你喜欢
      • 2017-11-03
      • 1970-01-01
      • 2023-03-09
      • 2020-02-14
      • 1970-01-01
      • 2016-03-02
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多