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

时间:2014-09-26 22:15:58

标签: 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 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”类的对象并传递给函数但它不起作用!

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);

它不能很好地传递物体! 你能帮我么? 感谢

1 个答案:

答案 0 :(得分:0)

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

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);

这是你的一个问题。