C++ 如何在c+中将多个对象作为参数传递给函数+;

C++ 如何在c+中将多个对象作为参数传递给函数+;,c++,C++,我有一个如下的类,它的方法接受一个对象作为它的参数 template <typename T> class Matrix{ private: matrixNode<T>* rear=0; std::vector<int> rows; std::vector<int> cells; std::vector<matrixNode<T>*> listofAddedNodes; void ge

我有一个如下的类,它的方法接受一个对象作为它的参数

template <typename T> class Matrix{
private:
    matrixNode<T>* rear=0;
    std::vector<int> rows;
    std::vector<int> cells;
    std::vector<matrixNode<T>*> listofAddedNodes;
    void generatetableRowCellArrays(int rowSize,int cellSize){
        for (int i=0;i<rowSize;i++){
            rows.push_back(i);
        }
        for (int i=0;i<cellSize;i++){
            cells.push_back(i);
        }
    }
public: 
    Matrix(int rowSize,int cellSize){
        generatetableRowCellArrays(rowSize,cellSize);
    }

    void ceateSparsMatrix(matrixNode<T> *node){
        matrixNode<T> *mxNode=(matrixNode<T>*)malloc(sizeof(matrixNode<T>));
        mxNode=node;

        if(rear!=0){
           rear->PL=(matrixNode<T>*)&rows[mxNode->line];
           rear->PC=(matrixNode<T>*)&cells[mxNode->column];
        }

        rear=mxNode;

        listofAddedNodes.push_back(mxNode);
    }

    void ceateSparsMatrix(matrixNode<T> node,int SpecificRow,int specificCell){
        matrixNode<T> *mxNode=(matrixNode<T>*)malloc(sizeof(matrixNode<T>));
        mxNode=node;
        if(rear!=0){
           rear->PL=&rows[SpecificRow];
           rear->PC=&cells[specificCell];
        }
        rear=mxNode;
        listofAddedNodesPointers.push_back(&rear->value);
    }

    void showAllAddedNodes(){
        for(int i=0;i<listofAddedNodes.size();i++){
            printf("|----------------------------|\n");
            printf("|Value   | %d                 |\n",listofAddedNodesPointers[i]->value);
            printf("|Coll    | %d                 |\n",listofAddedNodesPointers[i]->column);
            printf("|Line    | %d                 |\n",listofAddedNodesPointers[i]->line);
            printf("|PC      | %d        |\n",listofAddedNodesPointers[i]->PC);
            printf("|PL      | %d        |\n",listofAddedNodesPointers[i]->PL);
            printf("|----------------------------|\n");

        }
    }};};
模板类矩阵{
私人:
matrixNode*rear=0;
std::向量行;
std::载体细胞;
std::附加节点的向量列表;
void generateTableRowSize(int rowSize,int cellSize){
对于(int i=0;iline];
后->PC=(matrixNode*)和单元格[mxNode->column];
}
后方=节点;
添加节点列表。推回(mxNode);
}
无效矩阵(矩阵节点、int-SpecificRow、int-specificCell){
matrixNode*mxNode=(matrixNode*)malloc(sizeof(matrixNode));
mxNode=节点;
如果(后!=0){
后->PL=&行[特定行];
后->PC=&单元[特定单元];
}
后方=节点;
添加节点指针列表。向后推(&rear->value);
}
void showalladdendnodes(){
对于(int i=0;ivalue);
printf(“|Coll |%d |\n”,listofAddedNodesPointers[i]->列);
printf(“| Line |%d |\n”,listofAddedNodesPointers[i]->Line);
printf(“|PC |%d |\n”,添加节点指针列表[i]->PC);
printf(“|PL |%d |\n”,listofAddedNodesPointers[i]->PL);
printf(“|-----------------------------|\n”);
}
}};};
当我想测试这个函数时,我创建了几个“matrixNode”类的对象并传递给函数,但它不起作用

Matrix<int> matrix(5,6);

  matrixNode<int> *matNode();
  matNode->column=2;
  matNode->line=0;
  matNode->value=2;
  matNode->PC=NULL;
  matNode->PL=NULL;

  matrixNode<int> *matNode2;
  matNode2->column=5;
  matNode2->line=5;
  matNode2->value=5;
  matNode2->PC=NULL;
  matNode2->PL=NULL;

 matrix.ceateSparsMatrix(matNode);
 matrix.ceateSparsMatrix(matNode2);
矩阵(5,6);
matrixNode*matNode();
matNode->column=2;
matNode->line=0;
matNode->value=2;
matNode->PC=NULL;
matNode->PL=NULL;
matrixNode*matNode2;
matNode2->column=5;
matNode2->line=5;
matNode2->value=5;
matNode2->PC=NULL;
matNode2->PL=NULL;
矩阵。矩阵(matNode);
矩阵。矩阵(matNode2);
它不能很好地通过物体! 你能帮帮我吗?
谢谢

您需要动态分配矩阵对象或向现有对象传递指针

Matrix<int> matrix(5,6);

  matrixNode<int> * matNode = new matrixNode<int>;
  matNode->column=2;
  matNode->line=0;
  matNode->value=2;
  matNode->PC=NULL;
  matNode->PL=NULL;

  matrixNode<int> * matNode2 = new matrixNode<int>;
  matNode2->column=5;
  matNode2->line=5;
  matNode2->value=5;
  matNode2->PC=NULL;
  matNode2->PL=NULL;

  matrixNode<int> matNode3;
  matNode3.column=5;
  matNode3.line=3;
  matNode3.value=17;
  matNode3.PC=NULL;
  matNode3.PL=NULL;

 matrix.ceateSparsMatrix(matNode);
 matrix.ceateSparsMatrix(matNode2);
 matrix.ceateSparsMatrix(&matNode3);
矩阵(5,6);
matrixNode*matNode=新matrixNode;
matNode->column=2;
matNode->line=0;
matNode->value=2;
matNode->PC=NULL;
matNode->PL=NULL;
matrixNode*matNode2=新matrixNode;
matNode2->column=5;
matNode2->line=5;
matNode2->value=5;
matNode2->PC=NULL;
matNode2->PL=NULL;
矩阵节点matNode3;
matNode3.列=5;
matNode3.line=3;
matNode3.value=17;
matNode3.PC=NULL;
matNode3.PL=NULL;
矩阵。矩阵(matNode);
矩阵。矩阵(matNode2);
矩阵。矩阵(&matNode3);

这是您的问题之一。

什么叫“它不工作”?它应该将两个对象都传递给函数,但它只发送第一个对象!您没有在那里创建任何对象;您正在声明一个函数(当您试图将其视为指针时会出现编译错误),然后创建一个未初始化的指针(当您尝试去引用它时,会给出运行时错误)。尝试<代码> MatrxNo.MatLoad;< /C> >如果您想创建对象。删除所有的星体字符,并在函数定义中添加“&”BrULLUE(或后继)参数。当C++中没有其他可能的情况时,指针将被使用。总是尝试使用引用;您可以创建指向内存中任何位置的对象的指针,您知道。。。