2011-10-26 82 views
2

嗨我一直在尋找修復這個問題的地方,並嘗試了多種不同的方式來定義ListNode。 cpp文件。出於某種原因,該結構不能與.cpp文件共享。任何幫助將非常感激。 由於不知道爲什麼私人會員從頭文件不在cpp文件中工作

.H FILE:

#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;     
} 

RELEVANT ERRORS: C:\用戶\查爾斯\桌面\ SortedList.cpp在功能Student SortedList::* find(int)': 12 C:\Users\Charles\Desktop\SortedList.cpp ListNode」未申報(首次使用此功能)

+0

是那真的是你從代碼中得到的* first *錯誤?始終按順序處理錯誤。當編譯器遇到錯誤時,它會對你的意思做出某些假設,以便繼續編譯並給出更多的錯誤消息,但有時候,後面的錯誤只是編譯器錯誤假設的副作用。如果您開始處理列表中間的錯誤,那麼最終可能會浪費時間來修復那些並非真正錯誤的事情。始終從頂部開始。 –

+1

上面是很好的建議,但是我自己測試編譯了這個(使用Student的存根定義),它確實是第一個錯誤。 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) 
{ 

相關問題