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:
#include"5.hpp"
#include<iostream>
#include<fstream>
#include<istream>
#include<cmath>
usingnamespace 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.
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 = newdouble**[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?