c ++列出类作为学生类型

时间:2013-07-03 16:59:07

标签: c++

我正在尝试使用类Student并将其声明为列表类型。我可以推回但不更改List.h或Node.h如何在list2中打印数据? List..h中给定的print()函数不起作用:(

Node.h

#ifndef NODE_H
#define NODE_H
#include <string>
#include <iostream>
using namespace std;
template <typename T>
class Node {
private:
  T data;
  Node<T>* next;
public:
  Node(T);
  virtual ~Node(); // base class destructor must be virtual

  template <typename U> friend class List;
};
template <typename T>
Node<T>::Node(T d) {
  data = d;
  next = NULL;
}
template <typename T>
Node<T>::~Node() {
}
#endif  /* STRNODE_H */

List.h

#ifndef LIST_H
#define LIST_H
#include "Node.h"

// Singly linked list
template <typename T>
class List {
private:
  Node<T>* head; // pointer to the first node
  Node<T>* tail; // pointer to the last node
  int count;  // number of nodes in the list
public:
   class OutOfRangeException{ }; // empty inner class for exception handling
   List();
   virtual ~List();
   void push_back(T item);
   void insert(int index, T item);
   void remove(int index);
   int indexOf(T item);
   T get(int position); // OutOfRangeException is generated
   bool isEmpty();
   int size();
   void print();
  };
 template <typename T>
 List<T>::List() {
 head = tail = NULL;
  count = 0;
 }
 template <typename T>
 List<T>::~List() {
 Node<T>* discard;
 while (head != 0) {
 discard = head;
 head = head->next;
 delete discard;
 }
 } 

 // append an item at the end of the StrList
 template <typename T>
 void List<T>::push_back(T item) {
 try {
 Node<T>* newNode = new Node<T>(item);
 if (head == 0) {
  head = tail = newNode;
   } else {
   tail->next = newNode;
   tail = newNode;
  }
   ++count;
  } catch (bad_alloc &e) {
   cout << "memory allocation exception: " << e.what() << endl;
 exit(1);
 }
 }

 // insert an item at the specified index
 template <typename T>
 void List<T>::insert(int index, T item) {
 try {
 if (index < 0 || index > count) // push_back() if index == count
  throw OutOfRangeException();

  Node<T>* newNode = new Node<T>(item);
  if (head == 0) { // empty
  head = tail = newNode;
  } else if (index == 0) { // at the start
  newNode->next = head;
  head = newNode;
  } else if (index == count) { // at the end
  tail->next = newNode;
  tail = newNode;
  } else { // insert in the middle
  Node<T>* prevNode;
  Node<T>* currNode = head;
  for (int i = 0; i < index; i++) {
    prevNode = currNode;
    currNode = currNode->next;
  }
  // insert between 'prevNode' and 'currNode'
  prevNode->next = newNode;
  newNode->next = currNode;
 }
  ++count;

 } catch (bad_alloc &e) {
 cout << "memory allocation exception: " << e.what() << endl;
  exit(1);
 }
 }

 // is the StrList empty?
  template <typename T>
  bool List<T>::isEmpty() {
  return count == 0;
   }

  // remove the item at specified index
  template <typename T>
  void List<T>::remove(int index) {
   if (index < 0 || index >= count)
   throw OutOfRangeException();

   if (index == 0) { // at the start
    Node<T>* discard = head;
    head = head->next;
    delete discard;
     } else {
    Node<T>* prevNode;
    Node<T>* currNode = head;
    for (int i = 0; i < index; i++) {
     prevNode = currNode;
     currNode = currNode->next;
    }
   // remove 'currNode'
    prevNode->next = currNode->next; // bypass
     delete currNode;

    if (index == count - 1) // last node was removed. Update 'tail'
    tail = prevNode;
    }
    --count;
   if (count == 0)
   tail = NULL;
   }

 // retrieve the item at the given position of the StrList. position starts from 0.
  // throws OutOfRangeException if invalid position value is given.
  template <typename T>
 T List<T>::get(int position) {
  if (position < 0 || position >= count)
  throw OutOfRangeException();

    int loc = 0;
     Node<T>* curr = head;
     while (loc < position) {
    ++loc;
   curr = curr->next;
    }
   return curr->data;
   }

  // Requirement:
  //   != operator of <class T>  is used
  template <typename T>
 int List<T>::indexOf(T item) {
   if (head == 0) {
   return -1; // not found
  } else {
  int index = 0;
   Node<T>* currNode = head;
    while (currNode->data != item && currNode != NULL) {
   currNode = currNode->next;
     ++index;
  }
   if (currNode == NULL) // not found thru the end
     return -1;
  else
    return index;
 }
 }

  // number of nodes in the StrList
  template <typename T>
  int List<T>::size() {
   return count;
  }

 // Requirement:
 //   << operator for <class T> is used.
  template <typename T>
 void List<T>::print() {
  cout << "*** StrList contents ***" << endl;
   for (int i = 0; i < count; i++) {
   cout << i << ": " << get(i) << endl;
    }
  }
 #endif

Student.h

#include "List.h"

class Student {
 private:
string name;
int id;
public:
Student();
Student(string a);
virtual ~Student();
friend ostream& operator<<(ostream &os, const Student& p);
bool operator!=(const Student &p) const;
bool operator==(const Student &p) const;
};
Student::Student() {
}
Student::Student(string a) {
name = a;

}
 Student::~Student() {

}
ostream& operator<<(ostream &os, const Student& p) {
return os << p.name;
}
 bool Student::operator==(const Student &p) const {
 // Compare the values, and return a bool result.
if (name == p.name)
    return true;
else
return false;
}
 bool Student::operator!=(const Student &p) const {
return !(*this == p);
 }

的main.cpp

 #include <iostream>
 using namespace std;

 #include "Student.h"

 int main() {

  cout << "\n*** StrList Test ***" << endl;

 List<string> list;
 list.push_back("zero");
 list.push_back("one");
 list.push_back("two");
 list.push_back("three");
 list.push_back("four");
 list.push_back("five");
 list.print();

  list.insert(1, "inserted at position 1");
  list.insert(0, "inserted at position 0");
  list.insert(4, "inserted at position 4");
  list.print();

  cout << "removing at indexes 3, 0" << endl;
  list.remove(3);
  list.remove(0);
 list.print();

 list.insert(2, "inserted at position 2");
 list.print();

   cout << "five is at index " << list.indexOf("five") << endl;
  cout << "two is at index " << list.indexOf("two") << endl;

   //Test for my Student class implementation
  //  Student<string> st1; //Create new student Ryan Martin with id of 1
  List<Student> list2;
  Student stu("Ryan Martin");
   list2.push_back(stu);
  //list2.print();
  //list2.push_back("Ryan");
  //list2.PrintStudents(); //Test that the Student class successfully stored and can          access
  return 0;

 }

3 个答案:

答案 0 :(得分:1)

如果我理解正确,那么你想为你的学生班定义operator<<,你可以这样做:

friend std::ostream & operator<<(std::ostream & os, const Student & s)
{
  return os << s.name << " " << s.id << std::endl;
}

但是请注意,我没有测试过此代码,而且我还没有阅读您发布的所有代码段, 所以我可能错误地理解了你。

编辑: 因此,在使用Visual Studio进行尝试之后,您的学生类的完整版应该是这样的:

#include "List.h"

class Student {
  private:
    string name;
    int id;
  public:
    Student();
    Student(string a);
    virtual ~Student();

    friend std::ostream & operator<<(std::ostream & os, const Student & s)
    {
      return os << s.name << " " << s.id << std::endl;
    }
};

Student::Student() {
 }

Student::Student(string a) {
name = a;
}
Student::~Student() {
}

除非你打算将它作为其他类的基类,否则你不必在Student虚拟中创建析构函数。

答案 1 :(得分:1)

&lt;&lt;必须为您的Student类定义运算符。引用List.h:

// Requirement:
 //   << operator for <class T> is used.
  template <typename T>
 void List<T>::print() {
  cout << "*** StrList contents ***" << endl;
   for (int i = 0; i < count; i++) {
   cout << i << ": " << get(i) << endl;
    }
  }

因此,在您的Student课程中,您需要实现运算符&lt;&lt;(ostream&amp; out);

做朋友(朋友很有趣!):

friend std::ostream& operator<< (std::ostream &out, const Student &stu)
{
    return out << stu.name << " id: " << stu.id << std::endl;
}

这是一个很好的参考: http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/

答案 2 :(得分:0)

打印功能需要操作员&lt;&lt;要在你的学生班上定义,而事实并非如此

所以定义学生将如何显示&lt;&lt;它应该工作!