problem with vector 2D

hello.
I try to create a program with std :: vector two-dimensional, and I have a problem.
the program compiles but gives me an error in the execution of "EXC_BAD_ACCES (code = 1)
the member function "power_function"
and I noted this:

this Any_power * 0x7fff5fbff860 0x00007fff5fbff860
size = (int) 3
count = (int) 0
a = (int) 2
b = (int) 2
power_container __1 :: std :: vector <std :: __1 :: vector <int, std :: allocator :: __1 <int>>, std :: __1 :: allocator <std :: __1 :: vector <int, std __1 <int> :: :: allocator >>> size = 0.



what I do not understand is why if I create the same function out of class "Any_power", the process works, but if I put the function in the class, does not work.
any help is appreciated.
thanks in advance


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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  #include <iostream>
#include <vector>

class Any_power
{
public:
	int        size;
	int        count;
	int        a;
	int        b;
	
	           std::vector < std::vector < int > > power_container ;                         // vector 2D for matrix construction
	
	explicit   Any_power(): size( 3 ), count( 0 ), a( 2 ), b( 2 ) {};                        // constructor
	explicit   Any_power( std::vector< std::vector < int > >& V_init );                      // constructor for matrix
	
	int        power_function( int A, int B, std::vector < std::vector< int > >& p_cont );   // member function for calculate power a^b
	void       find_egual_item( int A, int B, std::vector < std::vector< int > >& p_cont );  // find ugual item on the vector 2D
	int        cleaner( std:: vector < std::vector < int > >& fresh_and_clean );             // delete item = 0 to   vector2D
	void       printer( int C );                                                             // stampa il risultato
	void       power_engine_start();                                                         // funzione generatrice
};

/*
 *--------------------------------------------------------------*
 |      constructor for vector 2D                               |
 *--------------------------------------------------------------*
 */
inline           Any_power::Any_power( std::vector< std::vector < int > >& V_init  )
{
	for ( int i = 0; i < size; i++ )
	{
		V_init.push_back( std::vector < int > ( size ) );
		while ( 1 )
		{
			V_init.push_back( std::vector < int > ( size ) );
			break;
		}
	}
}

/*
 *--------------------------------------------------------------*
 |    member function for calculate power a^b                   |
 *--------------------------------------------------------------*
 */
inline int       Any_power::power_function( int A, int B, std::vector< std::vector< int > >& p_cont )
{
	int i( 2 ) , C( 1 ) , digits( 1 ) , k( 1 );
	
	p_cont[ A ][ 0 ] = 1;   // <<>-------### the problem  !!
	
	while ( C <= B )
	{
		C += i ;
		for ( int j = 2; j <= digits; j++ )
		{
			if ( k > 9 )
				p_cont[ A ][ j ] = p_cont[ A ][ j ] * p_cont[ A ][ j ] + ( k / 10 );
			else
				p_cont[ A ][ j ] *= p_cont[ A ][ j ];
			
			k = p_cont[ A ][ j ];
			
			if ( p_cont[ A ][ j ] > 9 )
			{
				p_cont[ A ][ j ] = p_cont[ A ][ j ] % 10;
				digits++;
			}
		}
		i++;
	}
	
	return 0;
}


/*
 *--------------------------------------------------------------*
 |          find ugual item on the vector 2D                    |
 *--------------------------------------------------------------*
 */

inline void      Any_power::find_egual_item( int A, int B, std::vector<std::vector<int> >& p_cont )
{
	for( int i = 0; i <= A; i++ )
      {
		for( int j = 0; j <= B; j++ ) //attenzione! il numero รจ composto da tutta la serie di j!! change pls
		{
			if( ( p_cont[ i ][ j ] != 0 ) && ( p_cont[ A ][ j ] == p_cont[ i ][ j ] ) )
			{
				p_cont[ A ][ j ] = 0;
			}
		}
      }
}

/*
 *--------------------------------------------------------------*
 |          delete item = 0 to   vector2D                       |
 *--------------------------------------------------------------*
 */

inline int       Any_power::cleaner( std::vector < std::vector < int > >& fresh_and_clean )
{
	for( int i = 0; i < fresh_and_clean.size(); i++)
      {
		for( int j = 0; j < fresh_and_clean[ i ].size(); j++ )
		{
			if( fresh_and_clean[ i ][ j ] == 0 )
			{
				for( j = 0; j < fresh_and_clean[ i ].size(); j++ )
				{
					fresh_and_clean.erase( fresh_and_clean.begin() + i );
				}
			}
			else
				count++;
		}
      }
	return count;
}

/*
 *--------------------------------------------------------------*
 |                     print result                             |
 *--------------------------------------------------------------*
 */

inline void      Any_power::printer( int C )
{
	std::cout << " the number of different objects is: " << C;
}

/*
 *--------------------------------------------------------------*
 |               funzione motore della classe                   |
 *--------------------------------------------------------------*
 */

inline void      Any_power::power_engine_start()
{
	std::cout << " v ";                                  // test1
	for( a = 2; a <= 100; a++ )
      {
		for( b = 2; b <= 100; b++ )
		{
			std::cout << " z ";                      // test2
			power_function( a, b, power_container );
		}
      }
	find_egual_item( a, b, power_container );
	printer( cleaner( power_container ) );
}
/*
 *--------------------------------------------------------------*
 |                 start programm                               |
 *--------------------------------------------------------------*
 */

int main()
{
	class Any_power object;
	object.power_engine_start();
	return 0;
}
First: power_container is a member variable. You do not need to pass it to a member function (like power_function())

In Any_power::power_engine_start() power_container is completely empty, hence assigning something will lead to a crash
thanks for the reply.
but could you explain why it remains empty?
Last edited on
Well, follow the flow: On line 163 the constructor on line 14 is called. power_container remains untouched until you assign something on line 51

Well, follow the flow: On line 163 the constructor on line 14 is called. power_container remains untouched until you assign something on line 51


hi code777. very sorry!
sorry but I did not notice your second answer.
I modified the second constructor by entering: ( line 12)
 
std::vector < std::vector < int > > power_container ( int A, int B, int V );

and: ( line 39 )
 
power_container ( a, b, v );


now the problem has moved to the lines 149-152-153, that give me the same type of error:
Reference to non-static member function must be called.

I read that to solve the problem, I would call "power_container with an instance of the class, but I have no idea how to do.
I tried to bring out of the class the "power_energy_start ()", then use the instance "object" class "Any_power", but the result does not change.

First of all you should use more descriptive variable names. It seems to me that a/A is column and b/B row?

Does that mean you want in power_engine_start() a matrix of the size 100 * 100? If so you need to tell it the vector (the vector has no chance to guess that):
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
inline int void      Any_power::power_function( int A, int B, std::vector< std::vector< int > >& p_cont )
{
	int i( 2 ) , C( 1 ) , digits( 1 ) , k( 1 );
	
	power_container[ A ][ 0 ] = 1;
	
	while ( C <= B )
	{
		C += i ;
		for ( int j = 2; j <= digits; j++ )
		{
			if ( k > 9 )
				power_container[ A ][ j ] = power_container[ A ][ j ] * power_container[ A ][ j ] + ( k / 10 );
			else
				power_container[ A ][ j ] *= power_container[ A ][ j ];
			
			k = power_container[ A ][ j ];
			
			if ( power_container[ A ][ j ] > 9 )
			{
				power_container[ A ][ j ] = power_container[ A ][ j ] % 10;
				digits++;
			}
		}
		i++;
	}
	
	return 0;
}

...

inline void      Any_power::power_engine_start()
{
	std::cout << " v ";                                  // test1
	power_container.clear(); // remove all previous stuff
	power_container.resize(100 + 1); // Note: + 1 due to a <= 100
	for( a = 2; a <= 100; a++ )
      {
		power_container[a].resize(100 + 1); // Note: + 1 due to b <= 100
		for( b = 2; b <= 100; b++ )
		{
			std::cout << " z ";                      // test2
			power_function( a, b, power_container );
		}
      }
...
}

hello code777.
Thank you for your help. I changed the code and I have avoided passing the vector to function.
now works.
I just have to make ends meet results.
If I have problems still take advantage of your consulting.
thank you very much!
Topic archived. No new replies allowed.