Help with pointers?

Hi! I am having so much trouble understanding how to use pointers. The comments in the header file describe what the variables/functions are supposed to do. I included what I have for the source file, which I think is a mess. I included comments in the source file which just explain why I thought to code it the way I did.
Can anyone help me figure out the flaws in my thought process? I've been watching youtube videos and reading C++ articles/forums for hours now and I just don't understand.

Also, why is it problem9* data instead of problem9 data? What is the purpose of the pointer? Is it to point to the memory location of where each new object is stored? If so, then is data holding the memory address?


header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//This class must remember how many instances of the class currently exist
class problem9 {
	public:
		//Total number of problem9s in existence
		static int total_problem9s;
		//Constructor sets number to the total number of problem9s in existence after this one is created
		problem9();
		~problem9();
};

class problem10 {
	private:
		problem9* data;
	public:
		//Constructor should initialize new memory for data
		problem10();
		//Destructor should give up memory used for data
		~problem10();

		//Get a reference to the value pointed to by data
		problem9& getValue();
};



source file:
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
problem9::problem9(){
	static int total_problem9s = 0;	// Initially set to 0.
	total_problem9s++;	// Increments the total number of problem 9 objects since the constructor is called every time an object is created
}

problem9::~problem9(){
	total_problem9s--;	// Decrements the total number of problem 9 objects since the deconstructor is called every time an object is deleted from memory
}




/* Problem 10 */

problem10::problem10(){
	data = new problem9&;	// data is a new problem9 object address. The error message says that you can't create a new address, though. So I know this isn't possible. I'm not sure what to do instead, though.
}

problem10::~problem10(){
	delete[] data;	// data is deleted, so the address is deleted
}

problem9& problem10::getValue(){

	return data;	// return the address.
}




Also, if anyone has any good links that helped with understanding pointers and memory addresses, I would greatly appreciate it. I always understand when it's the basics, but I can't seem to put it to practice in my code and it causes many problems.
Last edited on
Please see the tutorial section for a detailed and simple explanation of pointers, new and delete operators.

http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/dynamic/
After reading, I had a better understanding of what I need, but I'm still having trouble with finding what the getValue() function returns.

In header:
1
2
3
4
5
6
7
8
9
10
11
12
class problem10 {
	private:
		problem9* data;	// Data points to a problem 9 object.
	public:
		//Constructor should initialize new memory for data
		problem10();
		//Destructor should give up memory used for data
		~problem10();

		//Get a reference to the value pointed to by data
		problem9& getValue();
};



In source:
1
2
3
4
5
6
7
8
9
10
11
12
problem10::problem10(){

	data = new problem9;	// The data pointer points to a new problem9 object's newly allocated memory location
}

problem10::~problem10(){
	delete data;	// data is deleted, so the memory address of a problem9 object is now unassigned/empty.
}

problem9& problem10::getValue(){	// This function returns a reference to an address of a problem9 object
	return ???
}



I'm not sure what to return. The only thing I thought to return was data because it points to addresses of problem9 objects. However, data is a problem9* pointer type, and the function has to return the problem9& type.
Line 10 returns a reference to problem 9.
1
2
3
problem9& problem10::getValue()
{  return *data;
}


I would take exception to your comment on line 10. The function returns a reference to an object, not a reference to an address.

I would take exception to your comment on line 10. The function returns a reference to an object, not a reference to an address.

Thank you! Knowing that makes things so much clearer :D


When I changed getValue() to return *data, I started getting this error:

C:\Users\Kim\AppData\Local\Temp\ccw9znS1.o:homework6.cpp:(.text+0x29a): undefine
d reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccw9znS1.o:homework6.cpp:(.text+0x2a0): undefine
d reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccw9znS1.o:homework6.cpp:(.text+0x2b0): undefine
d reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccw9znS1.o:homework6.cpp:(.text+0x2b6): undefine
d reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccVt3O1l.o:homework6_main.cpp:(.text+0x873): und
efined reference to `void problem6_7<std::string>(std::string, std::string)'
C:\Users\Kim\AppData\Local\Temp\ccVt3O1l.o:homework6_main.cpp:(.text+0x8bc): und
efined reference to `void problem6_7<int, float>(int, float)'
C:\Users\Kim\AppData\Local\Temp\ccVt3O1l.o:homework6_main.cpp:(.text+0x97a): und
efined reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccVt3O1l.o:homework6_main.cpp:(.text+0x999): und
efined reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccVt3O1l.o:homework6_main.cpp:(.text+0x9db): und
efined reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccVt3O1l.o:homework6_main.cpp:(.text+0xa18): und
efined reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccVt3O1l.o:homework6_main.cpp:(.text+0xa71): und
efined reference to `problem9::total_problem9s'
C:\Users\Kim\AppData\Local\Temp\ccVt3O1l.o:homework6_main.cpp:(.text+0xa90): mor
e undefined references to `problem9::total_problem9s' follow
collect2.exe: error: ld returned 1 exit status




This is the main code in relation to problem9 and problem10:
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
//Creation sets total to 1, then to 2, then deletion brings it to 1 again
	int zero = problem9::total_problem9s;
	problem9 p9;
	int one = problem9::total_problem9s;
	problem9* p9_p = new problem9();
	int two = problem9::total_problem9s;
	delete p9_p;
	int one_again = problem9::total_problem9s;
	if (0 != zero or
			1 != one or
			2 != two or
		  1 != one_again) {
		std::cerr<<"problem9 failed, -15\n";
		score -= 15;
	}

	int two_again, now_three;
	{
		problem10 p10_1;
		two_again = problem9::total_problem9s;
		problem10 p10_2;
		now_three = problem9::total_problem9s;
	}
	int back_to_one = problem9::total_problem9s;
	if (2 != two_again or
			3 != now_three or
			1 != back_to_one) {
		std::cerr<<"problem10 failed, -15\n";
		score -= 15;
	}


Do I need to use problem9::total_problem9s in my problem9 constructor?

EDIT:
homework6.cpp: In constructor 'problem9::problem9()':
homework6.cpp:148:23: error: invalid use of qualified-name 'problem9::total_prob
lem9s'

^ I get that error when I use problem9::total_problem9s in my problem9 constructor


Here is problem9 now for reference:

header:
1
2
3
4
5
6
7
8
class problem9 {
	public:
		//Total number of problem9s in existence
		static int total_problem9s;
		//Constructor sets number to the total number of problem9s in existence after this one is created
		problem9();
		~problem9();
};


source:
1
2
3
4
5
6
7
8
problem9::problem9(){
	int problem9::total_problem9s = 0;
	total_problem9s++;	// Increments the total number of problem 9 objects since the constructor is called every time an object is created
}

problem9::~problem9(){
	total_problem9s--;	// Decrements the total number of problem 9 objects since the deconstructor is called every time an object is deleted from memory
}
Last edited on
Nevermind! I fixed it.

I took
int problem9::total_problem9s = 0;

out of my constructor, and declared it outside of the constructor in my source file.


Thanks for the help, guys!
Last edited on
Topic archived. No new replies allowed.