C++ c++;将班级列为学生类型
我正在尝试使用一个班级学生并将其声明为列表类型。我可以向后推,但不更改List.h或Node.h。如何打印列表2中的数据?List..h中给定的print()函数不起作用:( Node.hC++ c++;将班级列为学生类型,c++,C++,我正在尝试使用一个班级学生并将其声明为列表类型。我可以向后推,但不更改List.h或Node.h。如何打印列表2中的数据?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
#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 */
\ifndef节点
#定义节点
#包括
#包括
使用名称空间std;
模板
类节点{
私人:
T数据;
节点*下一步;
公众:
节点(T);
virtual~Node();//基类析构函数必须是虚拟的
模板好友类列表;
};
模板
Node::Node(td){
数据=d;
next=NULL;
}
模板
节点::~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
\ifndef列表
#定义列表
#包括“Node.h”
//单链表
模板
班级名单{
私人:
Node*head;//指向第一个节点的指针
Node*tail;//指向最后一个节点的指针
int count;//列表中的节点数
公众:
类OutOfRangeException{};//用于异常处理的内部类为空
List();
虚拟列表();
无效推回(T项);
无效插入(整数索引,T项);
无效删除(int索引);
int indexOf(T项);
T get(int位置);//生成OutOfRangeException
bool是空的();
int size();
作废打印();
};
模板
列表::列表(){
头=尾=空;
计数=0;
}
模板
列表::~List(){
节点*丢弃;
while(头!=0){
丢弃=头部;
头部=头部->下一步;
删除丢弃;
}
}
//在StrList的末尾追加一个项
模板
无效列表::推回(T项){
试一试{
节点*newNode=新节点(项);
如果(头==0){
头=尾=新节点;
}否则{
tail->next=newNode;
tail=newNode;
}
++计数;
}捕获(错误分配和评估){
cout-next=newNode;
tail=newNode;
}{ //插入中间
节点*prevNode;
节点*当前节点=头部;
对于(int i=0;inext;
}
//在“prevNode”和“currNode”之间插入
prevNode->next=newNode;
新建节点->下一步=当前节点;
}
++计数;
}捕获(错误分配和评估){
下一步;
}
//删除“currNode”
prevNode->next=currNode->next;//旁路
删除节点;
if(index==count-1)//删除了最后一个节点。更新'tail'
tail=prevNode;
}
--计数;
如果(计数=0)
tail=NULL;
}
//在StrList的给定位置检索项。位置从0开始。
//如果给定的位置值无效,则抛出OutOfRangeException。
模板
T List::get(int位置){
如果(位置<0 | |位置>=计数)
抛出OutOfRangeException();
int loc=0;
节点*curr=头部;
while(loc<位置){
++loc;
当前=当前->下一步;
}
返回当前->数据;
}
//要求:
//!=已使用的运算符
模板
int List::indexOf(T项){
如果(头==0){
return-1;//找不到
}否则{
int指数=0;
节点*当前节点=头部;
while(currNode->data!=项目&&currNode!=空){
currNode=currNode->next;
++指数;
}
if(currNode==NULL)//在结尾处找不到
返回-1;
其他的
收益指数;
}
}
//StrList中的节点数
模板
int List::size(){
返回计数;
}
//要求:
//如果我理解正确,那么您想定义操作符打印函数需要一个操作符确定,所以现在我的最后一个问题是实现一个!=操作符,因此如果有两个学生具有相同的名称和id,那么他们是相同的。我如何用我拥有的来测试它:添加一个bool操作符==或=或者两者都有(const-Stend&refS){return name==refS.name&&ID==refS.ID;}….在植入不相等的操作符时,请考虑使用| | |很抱歉,如果我没有遵循代码编写风格,我的手机会受到很大限制。
#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);
}
#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;
}
friend std::ostream & operator<<(std::ostream & os, const Student & s)
{
return os << s.name << " " << s.id << std::endl;
}
#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() {
}
// 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;
}
}
friend std::ostream& operator<< (std::ostream &out, const Student &stu)
{
return out << stu.name << " id: " << stu.id << std::endl;
}