无法弄清楚为什么头文件中的私有成员在 cpp 文件中不起作用
您好,我一直在到处寻找此问题的解决方案,并尝试了多种不同的方法在 .cpp 文件中定义 ListNode。由于某种原因,该结构无法与 .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;
}
相关错误: C:\Users\Charles\Desktop\SortedList.cpp 在函数 Student SortedList::* find(int)' 中: 12 C:\Users\Charles\Desktop\SortedList.cpp
ListNode'未声明(首先使用此函数)
Hi I've been searching all over the place for the fix to this issue and have tried multiple different ways of defining the ListNode in the .cpp file. For some reason the struct can't be shared with the .cpp file. Any help will be greatfully appreciated.
Thanks
.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 FILE:
#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:\Users\Charles\Desktop\SortedList.cpp In function Student SortedList::* find(int)':
ListNode' undeclared (first use this function)
12 C:\Users\Charles\Desktop\SortedList.cpp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这句话是错误的:
你把星星放在了错误的位置。这不是返回
Student*
的SortedList::find
定义的序言。这是名为find
的自由函数定义的序言,该函数返回Student SortedList::*
。 (一种不寻常但格式良好的“指向成员的指针”类型)。因为它不是 SortedList 成员方法,所以 SortedList 的内部声明都不在范围内,这就是为什么您会收到令人困惑的错误消息。这是你应该写的:(
没有必要将其分成三行,但会让其他人更容易阅读你的代码。)
This line is wrong:
You put the star in the wrong place. This is not the preamble to the definition of
SortedList::find
, returning aStudent*
. This is the preamble to the definition of a free function namedfind
, which returns aStudent SortedList::*
. (an unusual, but well-formed, "pointer-to-member" type). Because it's not a SortedList member method, none of SortedList's inner declarations are in scope, and that's why you get that confusing error message.This is what you should have written:
(Breaking it up into three lines like that is not necessary, but will make it easier for other people to read your code.)
您收到的编译器错误有点误导;您的问题与
ListNode
根本无关。您的 cpp 文件中存在语法错误:这意味着
SortedList::find
返回一个指向学生的指针。The compiler error you're getting is sort of misleading; your problem isn't related to
ListNode
at all. You've got a syntax error in your cpp file:This means that
SortedList::find
returns a pointer to a student.正确的签名是:
指针是返回类型的一部分,而不是方法名称的一部分。
The correct signature is:
The pointer is part of the return type, not part of the method name.