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

标签 c++

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

节点.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 */

列表.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

学生.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);
 }

主要.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;

 }

最佳答案

必须为 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 类中,你需要实现运算符<<(ostream &out);

做 friend ( friend 很有趣!):

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/

关于c++列表类作为学生类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17453963/

相关文章:

c++ - 预处理器 `_AFXDLL` 是什么意思?

c++ - 适用于 Visual Studio 的 OpenMP 3.0

c++ - 在 Windows 7 32 位上使用 Visual Express C++ 2012 设置 Qt 5

c++ - diab 5.7 编译器中的逻辑 AND 运算符返回非 bool 类型

c++ - QGraphicsDropShadowEffect 在嵌入式系统上占用 cpu

c++ - 为继承 enable_shared_from_this 的类获取 unique_ptr

c++ - 移动数组中字符串的位置

c++ - linux C/C++ socket程序为什么不出现ECONNRESET错误

c++ - 为什么不打印完整的小数?

c++ - 公共(public) "using"= decltype(<private>)