2011-10-26 3 views
2

안녕하세요. 저는이 문제에 대한 해결책을 찾기 위해 여러 곳에서 조사를 해왔으며 ListNode를 정의하는 여러 가지 방법을 시도했습니다. cpp 파일. 어떤 이유로 struct는 .cpp 파일과 공유 할 수 없습니다. 어떤 도움이라도 대단히 감사하겠습니다. 감사헤더 파일의 개인 회원이 cpp 파일에서 작동하지 않는 이유를 알아낼 수 없습니다.

.H 파일 :

#ifndef SORTEDLIST_H 
#define SORTEDLIST_H 

#include "Student.h" 

/* 
* SortedList class 
* 
* A SortedList is an ordered collection of Students. The Students are ordered 
* from lowest numbered student ID to highest numbered student ID. 
*/ 
class SortedList { 

    public: 

    SortedList(); 
    // Constructs an empty list. 

bool insert(Student *s); 
// If a student with the same ID is not already in the list, inserts 
// the given student into the list in the appropriate place and returns 
// true. If there is already a student in the list with the same ID 
// then the list is not changed and false is returned. 

Student *find(int studentID); 
// Searches the list for a student with the given student ID. If the 
// student is found, it is returned; if it is not found, NULL is returned. 

Student *remove(int studentID); 
// Searches the list for a student with the given student ID. If the 
// student is found, the student is removed from the list and returned; 
// if no student is found with the given ID, NULL is returned. 
// Note that the Student is NOT deleted - it is returned - however, 
// the removed list node should be deleted. 

void print() const; 
// Prints out the list of students to standard output. The students are 
// printed in order of student ID (from smallest to largest), one per line 

private: 

// Since ListNodes will only be used within the SortedList class, 
// we make it private. 
struct ListNode {  
    Student *student; 
    ListNode *next; 
}; 

ListNode *head; // pointer to first node in the list 
}; 

#endif 

.cpp 파일 :

#include <iostream> 
#include "SortedList.h" 

using namespace std; 


SortedList::SortedList() : head(NULL){} 



Student SortedList::*find(int studentID){ 
ListNode *current; 
current = head; 
if(current != NULL){ 
while(current != NULL){ 
       if(current.student.getID() == studentID){ 
       return current.student; 
       } 
current = current.next; 
} 
} 
return NULL;     
} 

되는 관련 오류 : C : 기능 Student SortedList::* find(int)': 12 C:\Users\Charles\Desktop\SortedList.cpp ListNode '선언되지 않은에서 \ 사용자 \ 찰스 \ 바탕 화면 \ SortedList.cpp (이 기능을 처음 사용)

+0

인가 그 코드에서 실제로 발생한 * 첫 번째 오류입니까? 항상 순서대로 오류를 해결하십시오. 컴파일러에서 오류가 발생하면 컴파일을 계속 시도하고 더 많은 오류 메시지를 표시 할 수 있도록 의미가 무엇인지 가정합니다.하지만 나중에 오류는 컴파일러의 잘못된 가정의 부작용입니다. 목록의 중간에있는 오류에 대한 작업을 시작하면 실제로 오류가 아닌 것을 수정하려고 시간을 낭비하게됩니다. 항상 맨 위에서 시작하십시오. –

+1

위의 내용은 좋은 충고입니다. 그러나 필자는이 스텁 (stub) 정의를 사용하여 직접 테스트 컴파일 한 결과 첫 번째 오류가 발생했습니다. OP는 실제로 C++에서 발을 쏘기위한 목적으로 제공하는 정말로 비열한 총 중 하나를 발견 할 수있었습니다. – zwol

+0

그리고 내 조언은 다음과 같습니다. 앞으로는 이해할 수없는 또 다른 문제가 생겨 도움을 요청할 때 가능한 한 가장 작은 프로그램으로 코드를 자르십시오. *는 동일한 오류를 생성합니다. 거의 항상 하나의 파일과 20 줄 미만으로 가져올 수 있습니다.이 과정에서 문제가 무엇인지 깨닫게 될지도 모릅니다. 그렇지 않은 경우에도 사람들이 여러분을 도울 수 있습니다. – zwol

답변

1

컴파일러 오류는 오해의 소지가 있습니다. 귀하의 문제는 ListNode과 전혀 관련이 없습니다. 당신은 당신의 CPP 파일에서 구문 오류가있어 :

Student *SortedList::find(int studentID) 

SortedList::find 학생에 대한 포인터를 반환 것을 의미한다.

+0

기술적으로 오류가 아니라 잘못된 기능을 선언했습니다. – zwol

0

올바른 서명 :

Student* SortedList::find(int studentID) 

포인터는 메서드 이름의 일부가 아닌 반환 형식의 일부입니다.

+0

빠른 응답 주셔서 감사합니다 – chazzwa

2

이 줄은 잘못된 것입니다 :

Student SortedList::*find(int studentID) { 

당신은 잘못된 장소에있는 별을 넣어. 이것은 이 아니며, SortedList::find의 정의에 대한 전문은 Student*을 반환하고은 아닙니다. 이것은 무료 함수find이라는 정의의 전문으로 Student SortedList::*을 반환합니다. (비정상이지만 잘 형성된 "포인터에 대한 포인터"유형). 그것은 SortedList 멤버 메소드가 아니기 때문에, SortedList의 내부 선언이 범위 내에 없기 때문에 그 혼란스러운 에러 메시지를 얻게됩니다.

당신이 작성해야 무엇 : (. 그런 세 줄에 그것을 깨는 필요는 없지만, 쉽게 다른 사람의 코드를 읽을 것)
Student * 
SortedList::find(int studentID) 
{