12 Replies - 4005 Views - Last Post: 10 April 2010 - 12:29 AM Rate Topic: -----

#1 Guest_Tim*


Reputation:

Using linked list: Header/implementation/driver file problem

Posted 09 April 2010 - 11:20 PM

In my project, I have:
aList.h
aList.cpp

aStudent.h
aStudent.cpp

Driver.cpp


I am making a singly Linked List and all of my files are linked together correctly.

In my aStudent.h header file:
class aStudent {

public:

.
.
.
private:
int variable1;
int variable2;
int variable3;
};



In my aList.h header file:
class aList {

public:

aStudent *pointer(int variable1);
.
.
.
private:

struct node {
aStudent *a;
node *nxt;
};

};



These are all set up correctly, correct inclusions of each and correct use of ifndef/def. However, my problem is in the aList.cpp implementation file:
Line 64 aStudent aList::*find(int variable1)
Line 65 {
Line 66 node *q;
}


This is what I currently have, and I know it's wrong... the errors I am getting is:

66 C:\Dev-Cpp\aList.cpp `node' undeclared (first use this function)
66 C:\Dev-Cpp\aList.cpp `q' undeclared (first use this function)

I believe it is my scope that is incorrect but I am not sure what else to do (I have tried researching this problem for several hours to no avail). Any help would be appreciated, thanks.

Is This A Good Question/Topic? 1

Replies To: Using linked list: Header/implementation/driver file problem

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 09 April 2010 - 11:26 PM

Well, the errors are self explanatory, node and q are undeclared. Make sure that you include "aList.h" where you use the identifier "node".
Was This Post Helpful? 1
  • +
  • -

#3 kcamse0hs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 09 April 2010 - 11:29 PM

I assure you it is included correctly. node is of type struct in the private part of class aList.

#include "aList.h"

I am declaring a pointer variable q of this type.


This is the OP.

This post has been edited by kcamse0hs: 09 April 2010 - 11:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 09 April 2010 - 11:38 PM

What's the prototype for your "find" function? And if you don't mind, you could post the entire code, it would be easier to figure the problem.
Was This Post Helpful? 1
  • +
  • -

#5 kcamse0hs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 09 April 2010 - 11:43 PM

I tried to change the code in case other classmates viewed this site, however if they happen to wander to this site and use the code (even though it's still incomplete) it's their own loss. I will post the entire code now.

Keep in mind, it may be sloppy/buggy, however I am solely concerned with the problem I described in the OP. And thank you for taking a look.

SortedList.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



SortedList.cpp
#include "SortedList.h"
#include <iostream>
using std::cout;
using std::endl;

SortedList::SortedList()
{
 head = NULL;
}

bool SortedList::insert(Student *s)
{
 ListNode *p = head, *prev = head, *temp;
   
 if (head == NULL) 
 {
  p = new ListNode;
  p->student = s;
  p->next = NULL;
  head = p;
  return 1;
 }
 
 else 
 {
  while (p != NULL)
  {
   if (p->student->getID() < s->getID()) //insert after
       {
        prev = p;          //point to previous node
        p = p->next;       //point to next node
       }
   else
       {
        if (prev == p)
        { 
          cout << "in while loop"; //insert at head, break
          break;
          }
        }
   } //end while
} //end large else

  //insert at head case
  if (prev == p)
     {
      cout << "YEP";
      temp = new ListNode;
      temp->student = s;
      temp->next = prev;
      head = temp;
     }
  
  //insert after case
  else
      {
       temp = new ListNode;     
       temp->student = s;
       temp->next = p;
       prev->next = temp;
       }
}

Student SortedList::*find(int studentID)
{
 ListNode *q = head;
 
 if (head == NULL)
    cout << "search aborted; empty list.";
    
 else
 {
  while (q != NULL)
  {
   if (studentID == q->student->getID())
      return q->student;
   
   else
       q = q->next;     
  }
 return 0;
 }
} 

SortedList Student::*remove(int studentID)
{
 
}

void SortedList::print() const
{
 ListNode *q = head;
 
 if (head == NULL)
    cout << "print failed; empty list.";
    
 else
 {
  while (q != NULL)
  {
     std::cout << q->student->getID() << " " << q->student->getCredits() 
          << " " << q->student->getGPA() << endl;
          
     q = q->next;
  }
     
 }
     std::cout << endl;  
}



Student.h
#ifndef STUDENT_H
#define STUDENT_H

/*
 * Student class
 *
 * A Student object contains a student ID, the number of credits, and an
 * overall GPA.
 */
class Student {

  public:
    
    Student();
    // Constructs a default student with an ID of 0, 0 credits, and 0.0 GPA.

    Student(int ID);
    // Constructs a student with the given ID, 0 credits, and 0.0 GPA.

    Student(int ID, int cr, double grPtAv);
    // Constructs a student with the given ID, number of credits, and GPA.

    // Accessors
    int getID() const;       // returns the student ID
    int getCredits() const;  // returns the number of credits
    double getGPA() const;   // returns the GPA

    // Other methods

    void update(char grade, int cr);
    // Updates the total credits and overall GPA to take into account the
    // additions of the given letter grade in a course with the given number
    // of credits.  The update is done by first converting the letter grade
    // into a numeric value (A = 4.0, B = 3.0, etc.).  The new GPA is 
    // calculated using the formula:
    //
    //            (oldGPA * old_total_credits) + (numeric_grade * cr)
    //   newGPA = ---------------------------------------------------
    //                        old_total_credits + cr
    //
    // Finally, the total credits is updated (to old_total_credits + cr)

    void print() const;  
    // Prints out the student to standard output in the format:
    //   ID, credits, GPA
    // Note: the end-of-line is NOT printed after the student information 
    

  private:
    int studentID;
    int credits;
    double GPA;
};

#endif




Student.cpp
#include "Student.h"
#include <iostream>
using std::cout;
using std::endl;

Student::Student()
{
 studentID = 0;
 credits = 0;
 GPA = 0.0;
}

Student::Student(int ID)
{
 studentID = ID;
 credits = 0;
 GPA = 0.0;
}

Student::Student(int ID, int cr, double grPtAv)
{
 studentID = ID;
 credits = cr;
 GPA = grPtAv;
}

int Student::getID() const
{
 return studentID;
}

int Student::getCredits() const
{
 return credits;
}

double Student::getGPA() const
{
 return GPA;
}

void Student::update(char grade, int cr)
{
 double num_grade;
 
 if (grade == 'A')
    num_grade = 4.0;
 if (grade == 'B')
    num_grade = 3.0;
 if (grade = 'C')
    num_grade = 2.0;
 if (grade = 'D')
    num_grade = 1.0;
 else
    num_grade = 0.0;
    
 if (0 < cr && cr < 10)
 {
    GPA = (((GPA * credits) + (num_grade * cr)) / (credits + cr));
    credits += cr;
 }
 
 else
  std::cout << "error: credits must be within the range 1-9" << endl;
 
}

void Student::print() const
{
 std::cout << getID() << " " << getCredits() << " " << getGPA();
}
 




Driver.cpp
#include "student.h"
#include "sortedlist.h"
#include <iostream>
using namespace std;

void printHelp(char);

int main() {
 SortedList List;
 Student *s;
 
 int x, y;
 double z;
 char input, w;
 
 cout << "*** To access help use: ***" << endl 
      << "? <a,d,u,p>" << endl
      << "? <x> for all" << endl << endl;
      
 cin >> input;
 while (input != 'q')
 {
  if (input == 'a')
  {
   cin >> x >> y >> z;
   s = new Student(x, y, z);
   List.insert(s);
  }
  
  if (input == 'd')
  cout << "delete"; //delete
  
  if (input == 'u')
  cout << "update"; //update
  
  if (input == 'p')
     List.print();
  
  if (input == '?')
  {  cin >> w;
     printHelp(w);
  }
  
  cin >> input;
 }
 
 system ("pause");
 return 0;

}

void printHelp(char w)
{
 if (w == 'x')
 {
    cout << "syntax: a <id> <credits> <gpa>" << endl;
    cout << "syntax: d <id>" << endl;
    cout << "syntax: u <lettergrade> <credits>" << endl;
    cout << "syntax: p" << endl;
 }
 
 else
 {
  if (w == 'a')
    cout << "syntax: a <id> <credits> <gpa>" << endl;
  if (w == 'd')
    cout << "syntax: d <id>" << endl;
  if (w == 'u')
    cout << "syntax: u <lettergrade> <credits>" << endl;
  if (w == 'p')
    cout << "syntax: p" << endl;
 }

}



Student SortedList::*find(int studentID)

AND

SortedList Student::*remove(int studentID)

in the implementation file SortedList.cpp is where I'm having trouble.

This post has been edited by kcamse0hs: 09 April 2010 - 11:47 PM

Was This Post Helpful? 0
  • +
  • -

#6 kcamse0hs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 10 April 2010 - 12:00 AM

?
:>
Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 10 April 2010 - 12:09 AM

Nevermind, if you don't want the struct to be declared globally.

This post has been edited by sarmanu: 10 April 2010 - 12:19 AM

Was This Post Helpful? 1
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Using linked list: Header/implementation/driver file problem

Posted 10 April 2010 - 12:09 AM

Typo:

//Student SortedList::*find(int studentID)
Student *SortedList::find(int studentID)



Class student doesn't have a remove() method:

//SortedList Student::*remove(int studentID)
//SortedList *Student::remove(int studentID)
//{
//
//}



With those changes:

Quote

*** To access help use: ***
? <a,d,u,p>
? <x> for all

? x
syntax: a <id> <credits> <gpa>
syntax: d <id>
syntax: u <lettergrade> <credits>
syntax: p
p
print failed; empty list.
a 1234 5 5
p
1234 5 5

a

9090 3 4

p
1234 5 5
9090 3 4

This post has been edited by n8wxs: 10 April 2010 - 12:12 AM

Was This Post Helpful? 1
  • +
  • -

#9 kcamse0hs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 10 April 2010 - 12:11 AM

Both header files were created by the instructor of the class... I'm pretty sure he doesn't want us to globally declare the struct.

EDIT: However, I am pretty sure you just answered my question in making:

Student SortedList::*find(int studentID)

to

Student *SortedList::find(int studentID)


@n8wxs

I was guessing at that point, lol

DOUBLE EDIT: I guess another question would be, how would I access this find function from main?

This post has been edited by kcamse0hs: 10 April 2010 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

#10 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 10 April 2010 - 12:20 AM

List.find(ID_here) - that's how you access it. By the way, in your "find" function, q is undeclared. It is a pointer, poiting to nothing.
Was This Post Helpful? 1
  • +
  • -

#11 kcamse0hs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 10 April 2010 - 12:24 AM

Neg, check the function again. it points to head :D
Was This Post Helpful? 0
  • +
  • -

#12 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 10 April 2010 - 12:27 AM

Oh, ok. But why do I have it uninitialized? Maybe I removed it and I don't remember :-) Nevermind anyway, glad you got it working.
Was This Post Helpful? 1
  • +
  • -

#13 kcamse0hs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-December 09

Re: Using linked list: Header/implementation/driver file problem

Posted 10 April 2010 - 12:29 AM

Thanks much.

I just tested the program and everything works, but I've found a new logical error....

When I add the ID's, then add one within the range of the ID's.. program infinite loops...

Back to the drawing board heh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1