Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用不同的构造函数创建多个矩阵_C++_Matrix_Constructor_Destructor - Fatal编程技术网

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

C++ 使用不同的构造函数创建多个矩阵,c++,matrix,constructor,destructor,C++,Matrix,Constructor,Destructor,我有一个赋值,我需要使用T型元素的矩阵类。我有一个使用2 int的构造函数,一个复制构造函数,一个使用字符串和析构函数的构造函数。矩阵A使用第一个构造器,工作正常,矩阵B使用第二个构造器,也可以很好地打印出来,但我对矩阵C有一个问题。如果我A自己声明它(从主构造器中删除矩阵A和B),它也会打印出来,但当所有的构造器都存在时,它会打印出如下内容: 3735748 3745176 3745176 3735748 (而不是 5 6 7(8) 每次编译时,数字都会改变。 我觉得这应该是显而易见的,但我

我有一个赋值,我需要使用T型元素的矩阵类。我有一个使用2 int的构造函数,一个复制构造函数,一个使用字符串和析构函数的构造函数。矩阵A使用第一个构造器,工作正常,矩阵B使用第二个构造器,也可以很好地打印出来,但我对矩阵C有一个问题。如果我A自己声明它(从主构造器中删除矩阵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;

}
模板
类矩阵
{
私人:
unsigned int nLignes;//行数
无符号整数n克隆;//n克隆的nbr
代表;
公众:
矩阵(无符号整数nl,无符号整数nc)
{
设置对齐(nl);
塞特科隆(北卡罗来纳州);
代表=新的T*[nl];
对于(int i=0;i>i)
{
向量推回(i);
如果(ss.peek()==',')
忽略();
}
设置对齐(向量为(0));
塞特科隆(第(1)节);
int taille=nLignes*NCOLONES;
向量擦除(向量开始(),向量开始()+2);
如果(taille==vect.size()){//确保有足够的值
//填充数组
int n=0;
for(int i=0;i您忘了在构造函数中使用字符串为rep分配空间

此外,解构器有内存泄漏的风险,请您自己检查