Allocating dynamic memory class in void function

I am having trouble allocating dynamic memory to a class in a void function and later using it in the main function.
When I try to run it, I get the following message in lines 52 to 5:
"error: request for member 'set_value' in '*(Well + ((sizetype)(((unsigned int)n) * 4u)))', which is of pointed type 'WellSlice*' (maybe you meant to use '->' ?)"
I also get "error: invalid conversion from 'int' to 'int*' [-fpermissive]" on line 64 and "error: intializing argument 1 of 'void ReadInputData(int*, WellSlice**)' [-fpermissive]" on line 35.

PS: The Input.txt file contains the number of data points followed by 4 variables for each data point, such as:
5
10 25 30 0.5
11 30 40 1
9 25 5 10
6 87 3 2

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
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <new>
#include <fstream> 
#include <cstdio>
#include <string>
#include <istream>
#include <ios>
#include <iomanip>

using namespace std;


class WellSlice{
    double x,y, 
           pressure,
           flowrate;

public:
    void set_value (double, string);
    void print_values ();
};


void WellSlice::set_value (double value, string variable){
if (variable =="x1") {x = value;}
else if (variable =="y1"){y = value;}
else if (variable =="pressure1"){pressure = value;}
else if (variable =="flowrate1"){flowrate = value;}
}

void WellSlice::print_values (){ 
cout << x << ", " << y << ", " << pressure << ", " << flowrate;
}

void ReadInputData (int *i, WellSlice **Well){
    int n;
    ifstream InputData ("Input.txt");
    double x1, y1, pressure1, flowrate1;
    x1 = y1 = pressure1 = flowrate1 = 0;

    if (InputData.is_open()){
        int* iaux; iaux = (int*)i;
        InputData >> *iaux;
        *Well = new (nothrow) WellSlice[*i]; 

        for (n=0; n<*i; n++){    
            InputData >> x1;
            InputData >> y1;
            InputData >> pressure1;
            InputData >> flowrate1;
            if (InputData.eof() == true) break;  
            *Well[n].set_value(x1, "x1");
            *Well[n].set_value(y1, "y1");
            *Well[n].set_value(pressure1, "pressure1");
            *Well[n].set_value(flowrate1, "flowrate1");
        }
    }
}

int main(){
    WellSlice * Well;
    int i, n;

    ReadInputData (i, &Well);

    cout << "\nYou have entered: ";
    for (n=0; n<i; n++){
        cout << "\nWell slice number " << (n+1) << ":\n";
        Well[n].print_values();
    }
    delete[] Well;  

    return 0;

}


Last edited on
You essentually have: *( Well[n].set_value(x1, "x1") );
You want: (*Well[n]).set_value(x1, "x1");
better way: Well[n]->set_value(x1, "x1");
Thank you very much for your answer, that was precisely the problem I was having.

However, another problem has come up: For some reason, the program is crashing when I try to run it. I used many cout commands to find out that it is crashing when, for n = 1(the second run in the loop), it tries to read line 52.
Any ideas on where it might be going wrong?

Thank you very much!
Making your code obfuscated and then complaining that it doesn't work.

> *Well = new (nothrow) WellSlice[*i];
*Well (or if you prefer Well[0]) is pointing to the memory allocated

So you've allocated an array, the first element would be W[0][0] or (*W)[0]
the second element is W[0][1] or (*W)[1], that is not the same as (*W[1]) which is not dereferenceable.


Use std::vector
Topic archived. No new replies allowed.