使用具有继承性的类的双链表 [英] double linked list using classes with inheritence

查看:86
本文介绍了使用具有继承性的类的双链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在c ++中创建一个具有继承关系的双链表,该链表可以让用户输入有关出版物的数据.但是,在生成项目时,要输入发布的第一个值(isbn号),程序将中断并给出以下错误:

SDI2中的0x75045a0f处未处理的异常引荐工作1.exe:0xC0000005:访问冲突读取位置0x00000014.

它最初是说问题出在xstring中,但是在搜索了互联网之后,我改变了项目属性.

这是我的链表.h文件

I am currently trying to create a double linked list with inheritance in c++ which can have data input by the user about publications. The project builds however when it comes to entering the first value of the publication (isbn number) the program breaks giving the following error :

Unhandled exception at 0x75045a0f in SDI2 Referal work 1.exe: 0xC0000005: Access violation reading location 0x00000014.

It originally said the problem was in xstring but after searching the internet I changed the project properties from happening.

This is my linked list.h file

#include <iostream> 
#include <string>
using namespace std;

class publication
 {
 public:
	 string isbn;
	 string publisher;
 };
 class book: public publication
 {
 public:
	 string author;
	 string title;
 };
 class procedings: public publication
 {
 public:
	 string editor;
	 string date;
	 string title;

 };

 class node
 {
	 friend class linkedList;
	 friend class book;
 public:
	 node();
	 ~node();
	 void setNextNode(node *next);
	 node* getNextNode(void);
	 void setPrevNode(node *next);
	 node* getPrevNode(void);
	 void setData(int *data);
	 int* getData(void);

 //private:
	 node *next;
	 node *prev;
	 int *data;
	 book *book;
	 procedings *procedings;
 };

class linkedlist
{
	friend class node;
public:
	linkedlist();
	void insertFromFront();
	void insertFromEnd();
	void deleteSpecifiedNode();
//private:
	node *first;
	node *last;
	node *current;
};

 node::node()
 {
	 next = NULL;
	 prev = NULL;
	 data = NULL;
	 book = NULL;
	 procedings = NULL;
 }

 linkedlist::linkedlist()
 {
	 first = NULL;
	 last = NULL;
	 current = NULL;
 }

void linkedlist::insertFromFront()
 {
 
 node *temp; 
 temp = new node; 

 //cout << "Enter 1 for book or 2 for procedings: ";
 //cin >> temp->data;

 //if (temp->data == 1)
 //{
	cout << "Enter the book ISBN number: ";
	cin >> temp->book->isbn;
	cout << "Enter the book Publisher: ";
	cin >> temp->book->publisher;
	cout << "Enter the book Author: ";
	cin >> temp->book->author;
	cout << "Enter the book Title: ";
	cin >> temp->book->title;
 /*}
 else if (temp->data == 2)
 {
	 cout << "Enter the Procedings ISBN number: ";
	 cin >> temp->procedings->isbn;
	 cout << "Enter the Procedings Publisher: ";
	 cin >> temp->procedings->publisher;
	 cout << "Enter the Procedings Editor: ";
	 cin >> temp->procedings->editor;
	 cout << "Enter the Procedings Conference date: ";
	 cin >> temp->procedings->date;
	 cout << "Enter the Procedings Title: ";
	 cin >> temp->procedings->title;
 }
 else
 {
	cout << "Enter 1 for book or 2 for procedings: ";
	cin >> temp->data;
 }*/

 temp->next = first;
 first = temp;
 }

//void linkedlist::insert_from_end()
// {
// node *temp, *temp1;
//  
// temp = new node;
//  
// cout << "Enter data: ";
// cin >> temp->data;
// temp->next = NULL;
//  
// if(head == NULL)
// head = temp;
// else{
// temp1 = head;
// while(temp1->next != NULL)
// temp1 = temp1->next;
// temp1->next = temp;
// }
// }
//void linkedlist::delete_specified_node()
// {
// node *N2D; //node to delete
// node *temp; // keep track of the node before deleting
// int node_num;
//  
// N2D = head;
//  
// cout << "Enter node num:";
// cin >> node_num;
//  
// temp->next = N2D->next;
// delete N2D;
// }
// 
//
//  
</string></iostream>



这是我的主要cpp文件



this is my main cpp file

#include "stdafx.h"
#include "Linked List.h"
#include <iostream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int option;
	linkedlist database;
  
 do{
 cout << endl;
 cout << "Please select an option: " << endl;
 cout << "0. Exit the program." << endl;
 cout << "1. Insert a node to the head of the list." << endl;
 /*cout << "2. Insert a node to the end of the list." << endl;
 cout << "3. Delete specified_node. " << endl;*/
 cout << endl << " >> ";
 cin >> option;
  
 switch(option)
 {
 case 1: database.insertFromFront();
 break;
 /*case 2: database.insertFromEnd();
 break;
 case 3: database.deleteSpecifiedNode();
 break;*/
 }
 }while (option != 0);
 
	return 0;
}

</string></iostream>



我已注释掉许多代码,以尝试纠正所陈述的问题.

任何帮助,将不胜感激.在此先感谢:)



I have commented out alot of code in an attempt to rectify the problem stated.

Any help would be appreciated. thanks in advance :)

推荐答案

如果您只想要一个列表,为什么不使用std :: list?

无论如何,这里有一些建议:

1.您应该将用于输入节点的代码与用于将节点插入列表的代码分开-否则,对于插入节点的每个函数,都需要在其顶部,末端或其他任何位置复制几次该代码在列表的中间.

2.为此,您的插入函数应将节点作为参数.

3.插入后,您必须进行测试以检查列表是否仍然为空.结果代码将有所不同.相反,从列表中删除节点后,必须检查该节点是否为最后一个元素.

4.您的插入代码仅在向前方向上链接节点,而不向后链接.

5.我看不到为什么需要任何朋友声明,或者为什么需要将所有成员变量都公开.也许您这样做只是为了避免可能的错误源,但让我向您保证:限制可见性和访问权限的程度越多,您越能准确地查明错误源.
If you just want a list, why not use std::list?

Anyway, here''s some advice:

1. you should separate the code for entering a node from the code for inserting a node into the list - otherwise you need to copy that code several times, for each function that inserts a node, at the front, the end or anywhere else in the midst of the list.

2. Towards that end, your insert functions should take a node as parameter.

3. Upon insertion you must test to check whether the list is still empty or not. The resulting code will be different. Conversely, upon removing a node from the list, you must check whether that was the last element.

4. Your insert code does only link the nodes in forward direction, not backward.

5. I don''t see why you need any of the friend declarations, or why you needed to make all your member variables public. Maybe you did that just in an attempt to avoid possible sources of errors, but let me assure you: the more you restrict visibility and access rights, the better you will be able to pinpoint the source of errors.


在节点的构造函数中,您不构造(新)bookprocedings.

在您当前的设计中,它应该类似于:
In the constructor of the node, you do not construct (new) the book or the procedings.

In your current design, it should be something like:
node::node()
{
    next = NULL;
    prev = NULL;
    data = NULL;
    book = new book;
    procedings = new procedings;
}



在节点类中,您应该只有基类publication的一个成员;并由调用方负责分配变量.



In the node class, you should have only one member of the base class publication; and it the responsability of the caller to allocate the variable.

class node
 {
     friend class linkedList;
     friend class book;
 public:
     node();
     ~node();
     void setNextNode(node *next);
     node* getNextNode(void);
     void setPrevNode(node *next);
     node* getPrevNode(void);
     void setData(int *data);
     int* getData(void);

 //private:
     node *next;
     node *prev;
     int *data;
     publication* m_Publication;

 };



在代码中,您可以看到类似以下内容的内容:



and in the code, you can have something like :

temp = new node; 
publication* currentpublication = new publication;
node->publication = currentpublication;







or

temp = new node;
procedings* currentprocedings = new procedings;
node->publication = currentprocedings;


最多.


将您的成员保持私有状态,以确保指针的所有权.通过函数公开指向成员的指针:
Keep your members private to enshure the ownership of the pointers. Expose the pointers to members by functions:
class node
 {
     friend class linkedList;
     friend class book;
 public:
     node();
     ~node(){ if(pbook) delete pbook; }
     void setNextNode(node *next);
     node* getNextNode(void);
     void setPrevNode(node *next);
     node* getPrevNode(void);
     void setData(int *data);
     int* getData(void);

     // for example
     book*  getBook(){ if(!pbook) new book; return pbook; }
     void       setNext(node* p){ trail()->next=p; }

private:
   node*    trail(){ node* p; for(p=this;p->next;p=p->next); return p; }
     node *next;
     node *prev;
     int *data;
     book *pbook;
     procedings *procedings;
 };


问候.


这篇关于使用具有继承性的类的双链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆