Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
C++
#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>


this is my main cpp file

C++
#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 :)
Posted

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.
 
Share this answer
 
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:
C++
node::node()
{
    next = NULL;
    prev = NULL;
    data = NULL;
    book = new book;
    procedings = new procedings;
}


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.

C++
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 :

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


or

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

Max.
 
Share this answer
 
Comments
Stefan_Lang 25-Aug-11 10:29am    
Good points all! Maybe in light of replacing the two variables book and proceedings with just one you should revisit your suggestion for the node constructor? (it still constructs two objects)
Maximilien 25-Aug-11 11:25am    
yep, I did it in order of modifications to the original code.
Keep your members private to enshure the ownership of the pointers. Expose the pointers to members by functions:
C#
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;
 };

Regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900