chazzwa
chazzwa

Reputation: 25

Can't figure out why private member from header file isn't working in cpp file

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)': 12 C:\Users\Charles\Desktop\SortedList.cppListNode' undeclared (first use this function)

Upvotes: 2

Views: 1634

Answers (3)

Joe
Joe

Reputation: 42627

The correct signature is:

Student* SortedList::find(int studentID)

The pointer is part of the return type, not part of the method name.

Upvotes: 0

zwol
zwol

Reputation: 140669

This line is wrong:

Student SortedList::*find(int studentID) {

You put the star in the wrong place. This is not the preamble to the definition of SortedList::find, returning a Student*. This is the preamble to the definition of a free function named find, which returns a Student 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:

Student *
SortedList::find(int studentID)
{

(Breaking it up into three lines like that is not necessary, but will make it easier for other people to read your code.)

Upvotes: 2

mergeconflict
mergeconflict

Reputation: 8276

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:

Student *SortedList::find(int studentID)

This means that SortedList::find returns a pointer to a student.

Upvotes: 1

Related Questions