用不同的构造函数创建多个矩阵

时间:2014-12-03 03:07:47

标签: c++ matrix constructor destructor

我有一个赋值,我需要使用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;

}

1 个答案:

答案 0 :(得分:0)

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

此外,您的解构程序存在内存泄漏的风险,请自行检查。

相关问题