嗨我一直在尋找修復這個問題的地方,並嘗試了多種不同的方式來定義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」未申報(首次使用此功能)
是那真的是你從代碼中得到的* first *錯誤?始終按順序處理錯誤。當編譯器遇到錯誤時,它會對你的意思做出某些假設,以便繼續編譯並給出更多的錯誤消息,但有時候,後面的錯誤只是編譯器錯誤假設的副作用。如果您開始處理列表中間的錯誤,那麼最終可能會浪費時間來修復那些並非真正錯誤的事情。始終從頂部開始。 –
上面是很好的建議,但是我自己測試編譯了這個(使用Student的存根定義),它確實是第一個錯誤。 OP設法找到C++提供的真槍實彈的槍之一,以便在腳下射擊自己。 – zwol
下面是我自己的一些建議:將來,當你遇到另一個你不明白的問題,並且你想要求助時,你應該做的第一件事就是儘量減少你的代碼到最小的程序中*會產生相同的錯誤。幾乎總是可以將它歸結爲一個文件並少於20行。你很可能會意識到這個過程中的問題,即使你不這樣做,人們也會更容易幫助你。 – zwol