2011-10-26 16 views
2

こんにちは私はこの問題を修正するための場所を探していましたが、複数の異なる方法でListNodeを定義しようとしました。 cppファイル。なんらかの理由で、構造体を.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;     
} 

RELEVANTエラー: C:機能で\ Users \ユーザーチャールズ\デスクトップ\ SortedList.cpp Student SortedList::* find(int)': 12 C:\Users\Charles\Desktop\SortedList.cpp ListNode」宣言されていません(最初にこの機能を使用する)

+0

されています実際にそのコードから得た*最初のエラー?常にエラーの順序で作業してください。コンパイラがエラーに遭遇すると、それはあなたが何を意味しているかについてある仮定を行い、コンパイルを続行してより多くのエラーメッセージを与えることができますが、後のエラーはコンパイラからの悪い仮定の副作用です。リストの途中からエラーを処理し始めると、実際にはエラーではないものを修正しようと時間を無駄にしてしまうかもしれません。常に最上部から始めてください。 –

+1

上記は良いアドバイスですが、私はこれを自分自身でテストコンパイルしました(スタブの定義はStudentです)。これは本当に最初のエラーでした。 OPは、C++が自分自身を足で撃ち上げる目的のために提供する実際の卑劣な銃の1つを見つけることができました。 – zwol

+0

ここに私自身のアドバイスがあります:将来的に、あなたが理解していないもう一つの問題があり、助けを求めたいときは、できるだけ早くあなたのコードをできるだけ小さなプログラムにカットしようとします*は同じエラーを生成します。ほとんどの場合、1つのファイルに、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の内部宣言はスコープ内にありません。そのため、混乱しているエラーメッセージが表示されます。

これは、あなたが書かれているべきである:(。その必要はありませんが、それは簡単に他の人があなたのコードを読むことになりますように3行にそれを破る)

Student * 
SortedList::find(int studentID) 
{ 

関連する問題