2014-06-24 1 views
-1

여기에도 비슷한 질문을했지만 여전히 잘못 이해하고 있습니다. 도와주세요. 여기 http://pastebin.com/syZf3yM8C++ 디버그 어설 션 실패 _BLOCK_TYPE_IS_VALID (pHead-> nBlockUse)

오류 발생 :

내가 여기에 코드입니다 (파스칼처럼) 제한된 크기와 문자열 클래스에 대한 템플릿을 만들 필요가 소멸자를 my_string에서 http://i.stack.imgur.com/4Jo8i.png

+1

디버그 버전을 구축하고 디버거에서 실행으로 시작합니다. 디버거는 크래시 위치에서 멈추고 코드에 함수 호출 스택을 올려 놓습니다. 여기서 변수의 값을 검사 할 수 있습니다. –

답변

0

을, 당신의 코드는 다음과 같습니다

~my_string() { 
       delete [] this->text; 
     } 

텍스트는 포인터가 아니기 때문에 배열이 아니기 때문에 이렇게하면 안됩니다. *S3 = *S1 + *S2으로 전화하면 my_string <max_size, T> operator+(const my_string& s)이 호출됩니다. 이 연산자에서 my_string <max_size, T> res은 로컬 변수이며 스택에 있습니다. 이 기능이 끝나면 해상도는 자동으로 삭제됩니다. 따라서 S3에 res를 할당하는 것은 오류입니다. 이 같은 고정 할 수 있습니다

~my_string() { 

    } 

my_string <max_size, T> operator+(const my_string& s) { 
     my_string <max_size, T> res; 
     int count = 0; 
     while (this->get_char(count) != '\0') { 
      res.set_char(this->get_char(count), count); 
      count++; 
     } 

     for(int i = 0; count < max_size; i++){ 
      if (s.get_char(i) == '\0') { res.set_char('\0', count); break; } 
      res.set_char(s.get_char(i), count); 
      count++; 
     } 
     return res; 
    } 



my_string& operator=(const my_string& s){ 
     int size=s.get_length(); 
     for (int i = 0; i < size; i++){ 
      this->set_char(s.get_char(i),i); 
     } 
     return *this; 
    } 

를 마지막으로 주요 기능에 :

delete S1; 
delete S2; 
delete S3; 
관련 문제