2014-09-26 46 views
-1

我有一個類如低於其方法接受一個對象作爲其參數。如何幾個對象傳遞給函數作爲參數在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); 

它不能很好地傳遞對象! 你能幫我嗎? 感謝

+2

什麼是「它不工作」是什麼意思? – 2014-09-26 22:17:10

+0

它應該將兩個對象都傳遞給該函數,但它只是發送第一個對象! – 2014-09-26 22:18:54

+0

它只是「發送第一個對象」有多遠? – Nimelrian 2014-09-26 22:19:39

回答

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

這是您的一個問題。

相關問題