2014-09-26 6 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

"작동하지 않는다"는 것은 무엇을 의미합니까? –

+0

두 개체를 함수에 전달해야하지만 첫 번째 개체 만 보냅니다. –

+0

"첫 번째 물체를 보내려면"얼마나 멀리 있습니까? – Nimelrian

답변

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

귀하의 문제 중 하나입니다.

관련 문제