2014-10-20 2 views
0

그래서 정렬 된 연결된 목록을 만들었고 모든 재즈를 컴파일하고 첫 번째 항목을 잘 삽입합니다. 그러나 두 번째 항목을 삽입하려고하면이 오류가 발생합니다. [main] 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION 조금만 검색하고 찾았습니다. 그것은 아마 내 삽입 함수 또는 내 IsFull 함수 (나는 단지 목록에 10 개 항목을 삽입 할 수 있어야합니다)와 관련이있다 -하지만 내 인생에 나는 그것이 무엇이 잘못되었는지 알아낼 수 없습니다. 아마도 여기 누군가가 도울 수 있을까요? (코드 전체를 게시하는 것은 도움이 될 것입니다 경우에 나는 확실 해요,하지만 난 그렇게 어쨌든 할거야)정렬 된 목록에 항목을 삽입 할 때 문제가 있습니까?

NFL.h

#include <string> 
const int MAX_ITEMS = 10; 
enum RelationType {LESS, GREATER, EQUAL}; 

using namespace std; 
#ifndef NFL_H 
#define NFL_H 
struct NFL { 
     string firstName; 
     string lastName; 
     string currentTeam; 
     string position; 
     string school; 
}; 
#endif 

sortedNFL.cpp

#include "sortedNFL.h" 

struct NodeType 
{ 
    NFL player; 
    NodeType* next; 
}; 

SortedNFL::SortedNFL() // Class constructor 
{ 
    length = 0; 
    nflList = NULL; 
} 

bool SortedNFL::IsFull() const 
{ 
    if (length == MAX_ITEMS || length > MAX_ITEMS) 
    { 
    return true; 
    } 
    else 
    return false; 
    /*NodeType* location; 
    try 
    { 
    location = new NodeType; 
    delete location; 
    return false; 
    } 
    catch(std::bad_alloc exception) 
    { 
    return true; 
    }*/ 
} 

int SortedNFL::GetLength() const 
{ 
    return length; 
} 

void SortedNFL::MakeEmpty() 
{ 
    NodeType* tempPtr; 

    while (nflList != NULL) 
    { 
    tempPtr = nflList; 
    nflList = nflList->next; 
    delete tempPtr; 
    } 
    length = 0; 
} 

NFL SortedNFL::GetItem(NFL& playerRequested, bool& found) 
{ 
    bool moreToSearch; //flag for more items to search 
    NodeType* location; 

    location = nflList; //initial location is first item in NFL list 
    found = false; //flag for if item is found 
    moreToSearch = (location != NULL); 

    while (moreToSearch && !found) //while there is more to search and item is not found 
    { 
    if (playerRequested.lastName.compare(location->player.lastName) > 0) 
    { 
     location = location->next; 
     moreToSearch = (location != NULL); 
    } 
    if (playerRequested.lastName.compare(location->player.lastName) == 0 && playerRequested.firstName.compare(location->player.firstName) == 0) 
    { 
     found = true; 
     playerRequested = location->player; 
    } 
    if (playerRequested.lastName.compare(location->player.lastName) < 0) 
    { 
     moreToSearch = false; 
    } 
    } 
    return playerRequested; 
} 

void SortedNFL::PutItem(NFL inputPlayer) 
{ 
    NodeType* newNode; // pointer to node being inserted 
    NodeType* predLoc; // trailing pointer 
    NodeType* location; // traveling pointer 
    bool moreToSearch; 

    location = nflList; 
    predLoc = NULL; 
    moreToSearch = (location != NULL); 

    // Find insertion point. 
    while (moreToSearch) //while moreToSearch is true 
    { 
    if (inputPlayer.lastName.compare(location->player.lastName) > 0) 
    { 
     predLoc = location; 
     location = location->next; 
     moreToSearch = (location != NULL); 
    } 
    if (inputPlayer.lastName.compare(location->player.lastName) < 0) 
    { 
     moreToSearch = false; 
    }  
    } 

    // Prepare node for insertion 
    newNode = new NodeType; 
    newNode->player = inputPlayer; 
    // Insert node into list. 
    if (predLoc == NULL)   // Insert as first 
    { 
    newNode->next = nflList; 
    nflList = newNode; 
    } 
    else 
    { 
    newNode->next = location; 
    predLoc->next = newNode; 
    } 
    length++; 
} 
/*void SortedNFL::DeleteItem(NFL playerDeleted) 
{ 
    NodeType* location = nflList; 
    NodeType* tempLocation; 

    // Locate node to be deleted. 
    if (playerDeleted.lastName.ComparedTo(nflList->player) == EQUAL) 
    { 
    tempLocation = location; 
    nflList = nflList->next; // Delete first node. 
    } 
    else 
    { 
    while (playerDeleted.lastName.ComparedTo((location->next)->player) != EQUAL) 
     location = location->next; 

    // Delete node at location->next 
    tempLocation = location->next; 
    location->next = (location->next)->next; 
    } 
    delete tempLocation; 
    length--; 
}*/ 

void SortedNFL::ResetList() 
{ 
    currentPos = NULL; 
} 

NFL SortedNFL::GetNextItem() 
{ 
    NFL playerRequested; 
    if (currentPos == NULL) 
    currentPos = nflList; 
    playerRequested = currentPos->player; 
    currentPos = currentPos->next; 
    return playerRequested; 

} 
SortedNFL::~SortedNFL() //class destructor 
{ 
    NodeType* tempPtr; 

    while (nflList != NULL) 
    { 
    tempPtr = nflList; 
    nflList = nflList->next; 
    delete tempPtr; 
    } 
} 
+1

잠재적 인 문제는 위치가 null 일 수 있습니다. 여기에 null이 될 수 있습니다. if (inputPlayer.lastName.compare (location-> player.lastName) <0) – drescherjm

+0

@KerrekSB 터치 –

답변

1

하자를

if (inputPlayer.lastName.compare(location->player.lastName) > 0) 
{ 
    predLoc = location; 
    location = location->next; 
    moreToSearch = (location != NULL); 
} 

이의이 location 마지막을 가리키는 한 척하자 : 당신이를 GetItem()에있는 메인 루프보고 요소가 링크 목록에 있고 비교가 true으로 평가되었습니다. if 문 안에서 NULLlocation에 넣으십시오. 이것이 목록의 마지막 요소입니다.

그럼 어떻게 될까요?

if (inputPlayer.lastName.compare(location->player.lastName) < 0) 

붐. Null 포인터 역 참조. 정의되지 않은 동작입니다.

+0

아, 문제가 무엇인지 보았습니다. if "문이 아니라 if 문입니다. 감사! –

+1

@ TaM. 'inputPlayer.lastName.compare (location-> player.lastName)'를 호출하고 결과를 변수에 저장하는 것이 더 좋습니다. 이렇게하면 비교 * 한 번만 수행하고 단일 결과를 두 번 테스트합니다. 작성된 것처럼 첫 번째 조건이 실패하면 결과가 전혀 달라지지 않을 때 compare 메소드를 다시 호출하는 데 시간을 낭비하게됩니다. 'int compare_result = inputPlayer.lastName.compare (location-> player.lastName); if (compare_result> 0) {...} else if (compare_result <0) {...}'(그렇다면'else'를 잊어 버린 경우 버그가 없으며 추가 정수 비교 만 가능합니다.) – cdhowie

+0

@ 더 많은 이해가되는 cdhowie, 많은 감사합니다! –

관련 문제