Classes:memberfunction doesnt access right pointer

Hi,
my task is to print the dynamic array "tensor", which is can be accessed through a private pointer "***tensor". The function "print()" is a member-function of the class Tensor3S.
At first the program creates the dynamic array and fills it with floating-point numbers provided by the file "filename.txt". This is done by the non-standard constructor, Tensor3S(const unsigned int n, const char* filename), of the class Tensor3S.
Here is how I did it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;

class Tensor3S {
private:
  unsigned int n;            // Length Tensor
  double*** tensor;          // Pointer on tensor
  Tensor3S();                

public:
  Tensor3S(
      const unsigned int size, 
      const char* filename);    
  ~Tensor3S();  


  void print();	  
};


(This is the 5.cpp)
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
#include<iostream>
#include<iomanip>
#include "5.hpp"

using namespace std;

Tensor3S::Tensor3S(const unsigned int size, const char* filename){

	double ***tensor = new double**[size];
                 for(int i=0;i<size;i++){
		 tensor[i] = new double*[size];
 			for(int j=0;j<size;j++){
 			tensor[i][j] = new double[size];
 			}
 		}

	for(int i=0;i<size;i++){
	 for(int j=0;j<size;j++){
	  for(int k=0;k<size;k++){
	cin >> tensor[i][j][k];
// In this scope I can print the data via "cout << tensor[i][j][k];"
	  }
  }
	}
// Every adress can be printed
// These are for testing reasons and to remind myself how dynamic arrays and 
// the * work
cout 	<< "tensor=" << tensor << endl;
cout	<< "*tensor=" << *tensor << endl;
cout 	<< "**tensor=" << **tensor << endl;
cout 	<< "&(***tensor)=" << &(***tensor) << endl;


// And the print function

void Tensor3S::print(){

cout	<< "tensor=" << tensor << endl; // this gives another adress than
                                        // the tensor in the constructor
cout	<< "*tensor=" << *tensor << endl; // this too is another adress
//cout	<< **tensor << endl;              // core dumped



for(int i=0;i<3;i++){
	for(int j=0;j<3;j++){
		for(int k=0;k<3;k++){

cout	<< setw(5) << i
	<< setw(5) << j
	<< setw(5) << k
//	<< setw(12) << tensor[i][j][k] // core dumped
	<< endl;
				}
			}
		}
	}


The main.cpp looks like this:
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
#include"5.hpp"
#include<iostream>
#include<fstream>
#include<istream>
#include<cmath>
using namespace std;

int main(){

	int number_lines = 0;
	string line;
	ifstream myfile("filename.txt");

	while(getline(myfile, line)){
	++number_lines;
	}

cout	<< "#lines = " << number_lines << endl;  // #lines = 27


	int n = exp(log(number_lines)/3); // calculates the 3rd derivates for
                                          // which is the max length 
cout	<< "n=" << n << endl;             // n=3  

	char filename[8]; 



Tensor3S test(n, filename);

	test.print();


	return 0;
	}


Question now is:
How do I get the three dimensional array via pointer into the print-function?

Further notice:
5.hpp may not be edited.
Yes, this is an assignment.
I don't want the solution and only copy it, I want your advice what went wrong and get it done by myself.

Greeting,
/bored
Last edited on
The problem is on line 9 in 5.cpp: tensor is a local variable that hides the member variable tensor in 5.hpp. So in print() you use the member variable tensor which is not initialized
At first thanks for your help!
Just for me to understand better:
The code
1
2
double ***tensor = new double**[size];
// ...  

initialized a new local variable which was deleted after the program left the constructor.
As I wanted to write the adresses into the existing private variable or in this case pointer,
I should've used the variable I already declared (double*** tensor).
This is correct, right?

This means the correct code looks like:
1
2
3
4
5
6
7
tensor = new double**[size];
                 for(int i=0;i<size;i++){
		 tensor[i] = new double*[size];
 			for(int j=0;j<size;j++){
 			tensor[i][j] = new double[size];
 			}
 		}

Now everything works just fine. Thanks again!
Last edited on
This is correct, right?
Exactly
Topic archived. No new replies allowed.