C++ Can';我不明白为什么头文件中的私有成员是';t在cpp文件中工作
大家好,我一直在到处寻找解决此问题的方法,并尝试了多种不同的方法在.cpp文件中定义ListNode。由于某些原因,该结构无法与.cpp文件共享。任何帮助都将不胜感激。 谢谢 .H文件:C++ Can';我不明白为什么头文件中的私有成员是';t在cpp文件中工作,c++,header,private,member,C++,Header,Private,Member,大家好,我一直在到处寻找解决此问题的方法,并尝试了多种不同的方法在.cpp文件中定义ListNode。由于某些原因,该结构无法与.cpp文件共享。任何帮助都将不胜感激。 谢谢 .H文件: #ifndef SORTEDLIST_H #define SORTEDLIST_H #include "Student.h" /* * SortedList class * * A SortedList is an ordered collection of Students. The Studen
#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;
}
#包括
#包括“SortedList.h”
使用名称空间std;
SortedList::SortedList():head(NULL){}
学生分类列表::*查找(int studentID){
ListNode*当前;
电流=水头;
如果(当前!=NULL){
while(当前!=NULL){
if(current.student.getID()==studentID){
返回当前的.student;
}
当前=当前。下一步;
}
}
返回NULL;
}
相关错误:
C:\Users\Charles\Desktop\SortedList.cpp在函数Student SortedList::*find(int)':
12 C:\Users\Charles\Desktop\SortedList.cpp
ListNode'未声明(首先使用此功能)正确的签名是:
Student* SortedList::find(int studentID)
指针是返回类型的一部分,而不是方法名称的一部分。您得到的编译器错误有点误导;您的问题与
ListNode
完全无关。您的cpp文件中有语法错误:
Student*SortedList::find(int studentID)
这意味着SortedList::find
返回指向学生的指针。此行错误:
Student SortedList::*find(int studentID) {
你把星星放错地方了。这不是SortedList::find
定义的序言,返回一个Student*
。这是名为find
的自由函数定义的序言,该函数返回学生分类列表::*
。(一种不寻常但格式良好的“指向成员的指针”类型)。因为它不是SortedList成员方法,所以SortedList的内部声明都不在范围内,这就是为什么会出现令人困惑的错误消息
这是你应该写的:
Student *
SortedList::find(int studentID)
{
(像这样把代码分成三行是没有必要的,但会让其他人更容易阅读您的代码。)这真的是您从代码中得到的第一个错误吗?始终按顺序处理错误。当编译器遇到错误时,它会对您的意思做出某些假设,以便它可以继续尝试编译并向您提供更多错误消息,但有时,以后的错误只是编译器错误假设的副作用。如果您从列表的中间开始处理错误,那么您可能会浪费时间来修复那些不是真正的错误。以上是很好的建议,但是我自己测试编译了这个(使用Student的存根定义),这确实是第一个错误。奥普设法找到了一个真正狡猾的枪支,C++为射击你自己的脚提供了一个建议。下面是我自己的一些建议:在将来,当你有另一个你不理解的问题,你想寻求帮助时,您应该做的第一件事是尝试将代码缩减到可能产生相同错误的最小程序。几乎总是有可能把它压缩到一个文件,少于20行。你可能会意识到在这个过程中出现了什么问题,即使你没有意识到,人们也会更容易帮助你。感谢快速响应。从技术上讲,这不是语法错误,而是声明了错误的函数。