2013-03-31 4 views
0

내가 내 코드에서 객체가이처럼 보이는 "schemeList"를 불렀다 매개 변수 메모리 문제로 통과 ++ :C는 재귀 적으로

class scheme; 
class schemeList 
{ 
    friend class scheme; 
private: 

    scheme * head; 
    schemeList * tail; 

public: 
    schemeList(); 
    Token addScheme(vector <Token> &toAdd); 
    //...other functions 
}; 

나는에 문제가 있어요 기능은 가정된다 "addScheme"입니다 머리에 스킴을 추가하거나, 머리가 가득차면 꼬리에 꼬리말을 추가하십시오 (링크 된리스트에서).

Token schemeList::addScheme(vector<Token>& toAdd) 
{ 
    if (head->retCloseParen() != ')') 
    { 
     scheme * headCopy = head;  
     Token answer = head->addScheme(toAdd); 
     head = headCopy; 
     return answer; 
    } 
    else 
    { 
     //deal with tail 
     schemeList * arrow = this; 

     while (arrow->tail != NULL) 
     { 
      arrow = arrow->tail; 
     } 

     tail = new schemeList(); 

     tail->head= new scheme(); 
     tail->tail = NULL; 

     Token answer = arrow->tail->addScheme(toAdd); 
     return answer; 
    } 
} 

이 첫 번째 방식을 추가하기위한 잘 작동하지만 두 번째 체계라는 오류가 발생합니다 "처리되지 않은 예외 ..."여기에 지금까지 가지고있는 기능입니다. 전에 이런 문제가 있었지만 전체 객체를 전달하는 대신 변수 toAdd을 참조로 고정하여 해결했습니다. 왜 이것이 오류를 던지거나 해결할 수 있습니까?

class scheme 
{ 
    friend class identifierList; 

public: 
    Token * id; 
    char openParen; 
    char closeParen; 
    identifierList * idList; 

    scheme(); 
    //other functions 
}; 

class identifierList 
{ 
    friend class scheme; 
public: 
    Token * id; 
    identifierList * next; 

    identifierList(Token * inId, identifierList * inNext); 
}; 

그리고 토큰은 다음과 같습니다 : 경우

가이 일을 더 명확하게하는 방식이다

class Token 
{ 
    friend class datalogProgram; 

public: 
    int lineNumber; 
    string type; 
    string value; 

    Token(string inType, string inValue, int inLineNum); 
    //other functions 
}; 
+2

연결된 목록을 재 작성하는 데 시간을 낭비하지 말고'std :: list' 또는'std :: vector'를 사용해야합니다. –

+0

'scheme :: addScheme' 구현을 보여주세요. 그리고 예외를 얻는 호출 스택. – Paul

답변

0
  • 충분하지 않은 세부 사항은 주어, 예외를 일으키는 특히 코드 . 콜 스택을 더 잘 조사하십시오!
  • head 및 기타 포인터 변수를 NULL으로 초기화하고 있습니까?이 작업을 수행하는 생성자가 표시되지 않습니다. 어떤 수표도 없습니다 (단지 head->retCloseParen() != ')' 수표).
  • Thomas가 의견에서 제안한대로, 이미 사용할 수있는 일부 컨테이너 클래스를 더 잘 사용해야합니다.