2013-07-17 5 views
-2

다음 코드는 알파벳이없는 문자열을 찾습니다. 123 인식되어야하며, numberFinder()가사실뿐만 아니라 같은 수의 인덱스을 시작 반환하여야한다처럼 mynumber123 인식 할 수 없다 같은 경우와 numberFinder()는 허위 경우 반환해야 .액세스 위반 읽기 위치 0x00000006

생성자 :

for (i;i<m_length;i++) 
{ 
    bool UpperCaseBeforeNoFound=false; 

    if(this->numberFinder (i).second) 
    { 
     //do some calculations. 
    } 
} 
: 텍스트 파일 어딘가에 나는 다음과 같은 코드를 건너 구현에 따라

의 내용을 포함하는 문자열 m_text를 초기화

CaddressParser::CaddressParser(string fileName)  //constructor 
{ 
    m_fileName=fileName; 
    int length=getLength(m_fileName.c_str()); 
    m_text =fileReader(m_fileName.c_str()); 
    m_length=length; 
} 

numberFinder 함수는 다음과 같이 구현됩니다.

,
pair <int,bool> CaddressParser::numberFinder(int counter) 
{ 
    bool noFound=isdigit(m_text[counter]);  //number found? -> true 
    if(noFound) 
    { 
     int end=HouseNoDigits(counter); 
     if(((counter-1)>=0) && ((counter +end-1) <m_length)) 
     { 
      if((!(isalpha(m_text[counter-1]))) && (!isalpha(m_text[counter+end-1]))) 
      { 
       return make_pair(counter,noFound);  //return index if true 
      } 
     } 
    } 
    else return make_pair(0,noFound); 
} 

이제 문제는 다음과 같은 텍스트 "he23 시장 거리 런던 Q12 H13"를 포함하는 텍스트 파일입니다. 나는 제목에 언급 된 오류 및 디버거가 포함되어있는 라인에 저를 필요 :이 일어나는 이유를 알아낼 수 없습니다

if(this->numberFinder (i).second)

. 제발 이해해주세요.

+5

들여 쓰기가 읽기 쉽도록 수정하십시오. 감사. –

+1

아마도 "numberFinder"가 NULL을 반환합니다 (또는 'this'는 NULL입니다). –

+0

@ Oli Charlesworth 예! –

답변

0

일반적으로 액세스 위반 오류는 NULL 참조로 인해 발생합니다. 호출중인 함수 중 하나가 NULL 포인터에 액세스하려고합니다. isdigit 함수가 true 또는 false를 반환하는지 확인하십시오. m_text는 기존 메모리 위치를 가리 킵니다. 그렇지 않으면 메모리를 할당해야합니다. 또한 fileName이 NULL인지 확인해야합니다.

3

CaddressParser::numberFinder(int counter)에서이 조건이 실패 할 경우 :

if (counter - 1 >= 0 && counter + end - 1 < m_length) 

함수가 정의되지 않은 동작의 결과 값을 반환하지 않고 종료합니다.

함수에있는 조건문의 복잡성은 형식이 잘못되어 (적어도 질문에 게시 된 것처럼) 도움이되지 않습니다.

당신 수도는 else을 제거하여 필요한 동작을 얻을 수 있도록 모든 기본 pair 값을 반환합니다 '-을 통해 가을'(하지만 그건 당신이 정말 시나리오에 반환 할 값이 있는지에 따라 달라집니다) :

pair <int,bool> CaddressParser::numberFinder(int counter) 
{ 
    bool noFound=isdigit(m_text[counter]);  //number found? -> true 
    if(noFound) 
    { 
     // ... 
    } 

    return make_pair(0,noFound); 
} 
관련 문제