Access violation

Hi guys , I hate to ask it again but I have no choice since I have no idea how to solve it .

I am having this error and it crashed.

First-chance exception at 0x00c86e38 in Assignment 3.exe: 0xC0000005: Access violation writing location 0x00000088.
Unhandled exception at 0x00c86e38 in Assignment 3.exe: 0xC0000005: Access violation writing location 0x00000088.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "readfile.h"
#include "BTNode.h"
#include "BT.h"
#include "intersect.h"
#include "Queue.h"

using namespace std ;

void menu(){

	/.../ //intro stuff here
}

void function(customer &input , customer &input2, BT<customer>*tree,BT<customer>*tree2,BT<customer>*tree3,BTNode<customer>*t1,BTNode<customer>*t2,BTNode<customer>*t3){


char choice;

	cout<<"Please select function:";//prompt for input
	cin>> choice ;
	cout<<endl;

	switch (choice)
	{
	case '1':
		readfile(input,tree,t1);//readfile function
		system ("cls");
		menu();

/...../ //not important function here
	
	case '4':
		intersect(tree,tree2,tree3,input,t1,t2,t3);
		system ("cls");
		menu();
	
	//exit program stuff
}

int main()
{
	customer input,input2,input3;
	BTNode<customer> *t1=NULL,*t2=NULL,*t3=NULL;//I think the problem is the
	BT<customer>* tree= new BT<customer>;       //initialization here.
	BT<customer>* tree2=new BT<customer>;
	BT<customer>* tree3=new BT<customer>;
	int counter1=0,counter2=0;
	
	menu();

	function(input,input2,tree,tree2,tree3,t1,t2,t3);

	cout<<"the end ";

	cin.get();

	cin.get();

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "BT.h"
#include "readfile.h"

using namespace std ; 

void readfile(customer &input,BT<customer> *tree,BTNode<customer>* newnode)

{	string name;
	string file;
	string line;
	customer node1,node2;
	int number;
	double balance;

	cout<<"Please enter name of file:";
	cin>>name;

	file=name;
	cout<<endl;

	ifstream infile(file,ifstream::in) ;
	
	if(!infile.good())
	 {
		 cout<<"File 1 open error";
		 cout<<"\nPlease press any button to continue";
		 cin.get();
		 system("cls");
	 }
	
	else 
	{
		while (!infile.eof())
		 {
			 infile.ignore(256,'=');//ignore until '='
			 
			 infile>>number;//get account number
			 
			 newnode->item.account = number ;//problem starts here 

			 infile.ignore(8);

			 getline(infile,line);

			 newnode->item.name = line;//get name

			 if(line.size()>20) // Check if over 20 characters 
			 {
				cout<<"Client name is over 20 characters"<<endl; 
			 }

			 else {infile.ignore(10);

			 getline(infile,line);}

                         //blah blah get stuff

			 infile.ignore('\n');

			 tree->insert(node1,node2);
		 }
		
		infile.close();
		std::cout<<name<<" file read completed!";
		cin.get();
		cin.get();
	}
		
}

bool customer::operator>= (customer c2) {
	if (name.compare(c2.name) >= 0)
		return true;
	return false;
}

bool customer::operator== (customer c2) {
	if (name.compare(c2.name) == 0)
		return true;
	return false;
}

bool customer::operator> (customer c2) {
	if (name.compare(c2.name) > 0)
		return true;
	return false;
}

ostream& operator<<(ostream& out,const customer& A){
	out<<A.account<<endl;
	out<<A.name<<endl;
	out<<A.address<<endl;
	out<<A.dob<<endl;
	out<<A.balance<<endl;

	return out;
}


I'm really sorry I asked such stupid question , please help
Last edited on
Access violation on a memory address like 0x00000088 that is almost 0 suggest that you are trying to access an object through a null pointer.
Since I initialized the pointer to 0 I can understand that , but how do I solve it ?

I put it to be zero because the compiler says that it need to be initialized so I did , then I have no idea what to do with it
You're passing the null pointers into function (bad name, by the way) which, in turn passes them into either readfile or intersect.

Inside of readfile you're trying to access the BTNode pointer passed in but it's null.

Please sort out the function names and indentation!

EDIT: While I'm on the subject of pointers, it's worth making the point (pun intended) of safely accessing them! if (my_ptr) can save a lot of bother.
Last edited on
I tried to initialize the pointer with

BTNode<customer> *t1=new BTNode<customer>;

but it says


Error 1 error C2512: 'BTNode<T>' : no appropriate default constructor available

normally If i'm not doing a pointer I would just use

BTNode<customer> t1(input)//input is a customer variable

but in pointer I have no idea how to do it .

Just if someone ask

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef BTNode_type
#define BTNode_type

template <class T>
class BTNode {
	public:
		T			item;
		BTNode<T>	*left, *right;
		BTNode(T);
};

template <class T>
BTNode<T>::BTNode(T newItem) {
	item = newItem;
	left = right = NULL;
}

#endif 


This is BTNode
use
 
BTNode<customer> *t1=new BTNode<customer>(input);


this will call your constructor with the input argument.

when you use
 
BTNode<customer> *t1=new BTNode<customer>;


you are calling the constructor
 
BTNode<T>::BTNode();


which you don't have defined - hence the error message
Topic archived. No new replies allowed.