Define member variable in class to initialize vector size

Hello together,

I am relatively new to C++. After investing quite some time researching and
trying I am posting my problem here.

To illustrate the problem, i am posting what is working so far.
In the class initializePipeData values are hardcoded which are used
in another class.

1
2
3
4
5
6
7
8
9
10
11
12
13
  class initializePipeData {
	
public:
		
	int N_pipe = 200; 
	int N_flash = 1000;
	double flashTol = 1e-7;
	double blasiusExp = 0.2;
	double pipeRough = 0.00007;
	
private:
	
};


In the second class the main calculations are done, first a result matrix is
declared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class PipePressureDrop : public initializePipeData {
				
public:
			
PipePressureDrop::PipePressureDrop() : sVp(N_pipe, std::vector<double>(26)) {}
	

std::vector<std::vector<double>> dp_Pipe(double L_pipe, double alpha, double d_pipe...) {
		
for (int i = 0; i < N_pipe; i++) {
			
switch (i){
case 0:
				
sVp.at(0).at(0) = 0;	
...


So in order to fill the solution vector sVp the vector size N_pipe is needed. In case it is hard coded like in the example above it is working fine.

But what i want ist that int N_pipe, int N_flash etc. are received via a GUI.

I tried to do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class initializePipeData {
	
public:
	
	int N_pipe;
	
	void SetN(int n);
	int GetN(void);
	
	int N_flash = 1000;
	double flashTol = 1e-7;
	double blasiusExp = 0.2;
	double pipeRough = 0.00007;
	
private:
	
};


and outside the class:

1
2
3
4
5
6
7
8
void initializePipeData::SetN(int n) {
	N_pipe = n;
}

int initializePipeData::GetN(void) {
	return N_pipe;

}


and then in the body file the following to pass the value of N_pipe to the class:

1
2
3
initializePipeData iniD;

iniD.SetN(200);

It is compiling fine, but when executing i get the error:

Exception at 0x7552DAD8
Exception: std::length_error

I has to do with the declaration of the size of the solution vector.
Not sure if I am expressing myself and the problem right.

It would save my weekend if someone could help me ;-)
Thanks in advance!

Greetings
dajoao
Last edited on
Since N_pipe determines the size of the vector of vector, when you set N_pipe, simply resize the vector( and optionally initialize the new elements ), such that you have something 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
class InitializePipeData
{
protected:
    int N_pipe = 5;
    // the rest of the data members
protected:
    void setN( int n ){ N_pipe = n; }
    int getN() const { return N_pipe; }
};

class PipePressureDrop: public InitializePipeData
{
public:
    PipePressureDrop(): InitializePipeData{}, sVp( N_pipe, std::vector<double>( 5 ) ) { }
    void setNewPipeSize( int s ){
        int temp = N_pipe;

        sVp.resize( N_pipe = s );
        if( N_pipe > temp ){
            for( auto x = temp; x < sVp.size(); ++x ) sVp[x] = std::vector<double>( 5 );
        }
    }
};


Note that `s` in setNewPipeSize() may be values gotten from your UI. HTH
Thanks a lot, works like a charm!
Topic archived. No new replies allowed.