Reference/Pointer functions with a class/struct

So I have been working on this for a little while and I can't seem to figure out pointer and references inside of a function variable. I am also using a pointer to a struct inside one of the class functions and the remainder 2 functions are just called normally through the functions. Every time I run this it says I am accessing variables x and y and then crashed when it goes into one of the function passes.

Here is my .h file:

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

class Prog1Class {
public:
	Prog1Class();
	~Prog1Class();
	int PtrFunction(int*, double*);
	int RefFunction(int&, double&);
	void StructFunction(int*);
};

struct Prog1Struct {
	int m_iVal;
	double m_dVal;
	char m_sLine[81];
};

#endif 


And here is my CPP file with the main inside of it:

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.h>
#include <fstream>
#include "Prog1Class.h"
using namespace std;

Prog1Class::Prog1Class(){

}

Prog1Class::~Prog1Class(){

}

int Prog1Class::PtrFunction(int *a, double *b){
	cout << "Enter a non-decimal integer." << endl;
	cin >> *a;
	cout << "Enter a decimal integer." << endl;
	cin >> *b;
	cout << "You have entered: " << *a << endl;
	cout << "You have entered: " << *b << endl;
	return 0;
}

int Prog1Class::RefFunction(int &c, double &d){
	cout << "Please enter a non-decimal integer." << endl;
	cin >> c;
	cout << "Please enter a decimal integer." << endl;
	cin >> d;
	cout << "You have entered: " << c << endl;
	cout << "You have entered: " << d << endl;
	return 0;
}

void StructFunction(struct Prog1Struct *str){
	cout << "Enter an integer value." << endl;
	cin >> str->m_iVal;
	cout << "Enter a decimal integer value." << endl;
	cin >> str->m_dVal;
	cout << "Enter a string." << endl;
	//cin.getline(str->m_sLine, 81, '\n');
	//cout << str->m_sLine;
	cout << str->m_iVal << " " << str->m_dVal << endl << endl;
	return;
}

void main(){

	int* x;
	double* y;

	Prog1Class *cptr = new Prog1Class;
	Prog1Class class1;
	class1.PtrFunction(x,y);
	class1.RefFunction(*x,*y);

	Prog1Struct *str;
	str = new Prog1Struct();
	StructFunction(str);

	delete new Prog1Struct();
	delete new Prog1Class();

	system("pause");
}


I am also using Visual Studio 2010. Any help would be appreciated!
Last edited on
Your main function is the problem - you seem to have several misconceptions about how pointers and dynamic memory in C++ work.

First let's look at line 49 and 50 - these declare two pointers, but since you don't initialize them to point to anything, they just point to random memory. Why are they even pointer at all? Why do they need to be?

Why is line 52 a pointer?

Why are lines 57 and 58 on two lines instead of one like line 52?

Lines 61 and 62 actually create new objects and then delete them, then you leak the memory of the other objects you made.
Last edited on
I was told I needed to have a main that dynamically initialized a new of my struct and class.

StructFunction is supposed to have a pointer linking to the struct, PtrFunction is supposed to have a pointer to the function and then have the program query the user for input, and the RefFunction is supposed to do what PtrFunction did but with a reference pointer.

It's the only reason I assigned the pointer values for x and y to see if I could get it to work, but obviously it did not.

-Edit- I accidentally placed delete new for that last part and changed it to:

1
2
delete str;
delete cptr;


instead.
Last edited on
Topic archived. No new replies allowed.