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:
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.
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: