こんにちは私はこの問題を修正するための場所を探していましたが、複数の異なる方法で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」宣言されていません(最初にこの機能を使用する)
されています実際にそのコードから得た*最初のエラー?常にエラーの順序で作業してください。コンパイラがエラーに遭遇すると、それはあなたが何を意味しているかについてある仮定を行い、コンパイルを続行してより多くのエラーメッセージを与えることができますが、後のエラーはコンパイラからの悪い仮定の副作用です。リストの途中からエラーを処理し始めると、実際にはエラーではないものを修正しようと時間を無駄にしてしまうかもしれません。常に最上部から始めてください。 –
上記は良いアドバイスですが、私はこれを自分自身でテストコンパイルしました(スタブの定義はStudentです)。これは本当に最初のエラーでした。 OPは、C++が自分自身を足で撃ち上げる目的のために提供する実際の卑劣な銃の1つを見つけることができました。 – zwol
ここに私自身のアドバイスがあります:将来的に、あなたが理解していないもう一つの問題があり、助けを求めたいときは、できるだけ早くあなたのコードをできるだけ小さなプログラムにカットしようとします*は同じエラーを生成します。ほとんどの場合、1つのファイルに、20行未満で取得できます。あなたはこれをやり遂げる過程で何が問題なのかをよく知っているかもしれません。たとえそうでなくても、人々があなたを助けやすくなります。 – zwol