Results 1 to 7 of 7

Thread: TWO DIMENSIONNED ARRAY as an ARGUMENT for all the classes functions

  1. #1
    Join Date
    Feb 2012
    Posts
    24
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default TWO DIMENSIONNED ARRAY as an ARGUMENT for all the classes functions

    Hello ,


    I tried but with no success to pass a TWO DIMENSIONNED ARRAY , here called M, rows =cloumns=10,as an ARGUMENT for all the classes functions that I use in my program.
    Even after reading the articles posted in this website I could not make it .
    Here is my code:
    Qt Code:
    1. // headers
    2. #ifndef FENPRINCIPALE_H
    3. #define FENPRINCIPALE_H
    4. #include <QtGui>
    5. class FenPrincipale : public QWidget
    6. {
    7. Q_OBJECT
    8. public:
    9. FenPrincipale();
    10.  
    11. private slots:
    12. void genererCC(int **);
    13. void paintEvent(QPaintEvent *,int **);
    14.  
    15. private:
    16. QPainter *painter;
    17. int M [10][10];
    18. QPushButton *generer;
    19.  
    20. };
    21. #endif
    22. /////////////////Second Header//////////////////
    23. #ifndef FENCC_H
    24. #define FENCC_H
    25. #include <QtGui>
    26. class FenCC : public QDialog
    27. {
    28. public:
    29. FenCC ( QWidget *parent , int **);
    30. private:
    31. QPushButton *fermer;
    32. QPainter *painter;
    33. private slots:
    34. void paintEvent (QPaintEvent *, int **);
    35. };
    36. #endif
    37.  
    38. ///FenPrincipale.cpp
    39. #include "FenPrincipale.h"
    40. #include "FenCC.h"
    41. FenPrincipale::FenPrincipale()
    42. { int o,p;
    43. for (o=0; o<10; o++)
    44. {
    45. for (p=0; p<10; p++)
    46. {
    47. M[o][p]=0;
    48. }
    49. }
    50. M[0][3]=1;
    51. M[0][9]=1;
    52. M[2][6]=1;
    53. M[3][5]=1;
    54. M[5][3]=1;
    55. M[2][4]=1;
    56. M[4][2]=1;
    57. paintEvent(e,M);
    58. generer = new QPushButton("&Générer !",this);
    59. connect(generer, SIGNAL(clicked()), this, SLOT(genererCC(M)));
    60. }
    61. void FenPrincipale::genererCC(int **M)
    62. {
    63. FenCC *fenetreCC = new FenCC( this , M);
    64. fenetreCC->exec();
    65. }
    66. void FenPrincipale::paintEvent(QPaintEvent *e, int **M)
    67. {
    68. QPainter painter(this);
    69. int tour=0;
    70. int x=10;
    71. int y=10;
    72. int Num_Sommet=0;
    73. int T_Coordonnees [10][3];
    74. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    75. {
    76. (T_Coordonnees[Num_Sommet][0])=x;
    77. (T_Coordonnees[Num_Sommet][1])=y;
    78. (T_Coordonnees[Num_Sommet][2])=tour;
    79. if (tour==0)
    80. {
    81. x+=35;
    82. tour=1;
    83. }
    84. else
    85. {
    86. y+=55;
    87. tour=0;
    88. }
    89. }
    90. int w1=0;
    91. int w2=0;
    92. int cpt=10;
    93. Num_Sommet=0;
    94. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    95. {
    96. if ((T_Coordonnees[Num_Sommet][2])==1)
    97. {
    98. if(w1==0)
    99. {
    100. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+500) ;
    101. w1=1;
    102. }
    103. else
    104. {
    105. if(w1==1)
    106. {
    107. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+250+cpt) ;
    108. w1=0;
    109. cpt=cpt+15;
    110. }
    111. }
    112. if (Num_Sommet==9)
    113. {
    114. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    115. }
    116.  
    117. }
    118. else
    119. {
    120. if(w2==0)
    121. {
    122. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+200;
    123. w2=1;
    124. }
    125. else
    126. {
    127. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+100+cpt;
    128. w2=0;
    129. cpt=cpt+15;
    130. }
    131.  
    132. if (Num_Sommet==4)
    133. {
    134. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    135. }
    136. }
    137. painter.drawEllipse((T_Coordonnees[Num_Sommet][0]), (T_Coordonnees[Num_Sommet][1]), 30.0, 30.0);
    138.  
    139.  
    140. }
    141.  
    142. int i,j;
    143. for (i=0; i<10; i++)
    144. {
    145. for (j=0; j<10; j++)
    146. {
    147. if (M[i][j]==1)
    148. {
    149. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    150. }
    151. }
    152. }
    153.  
    154.  
    155.  
    156. for (i=0; i<10; i++)
    157. {
    158. for (j=0; j<10; j++)
    159. {
    160. if ((M[i][j]==1) && (M[j][i]==1))
    161. {
    162. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    163. if( (T_Coordonnees[i][1]>45)||(T_Coordonnees[j][1]>45) )
    164. {
    165. painter.drawLine(((T_Coordonnees[i][0]+20)-10),(T_Coordonnees[i][1]),((T_Coordonnees[j][0]+20)-10),(T_Coordonnees[j][1]));
    166. }
    167. else
    168. {
    169. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]+30),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]+30));
    170. }
    171. }
    172. }
    173. }
    174.  
    175.  
    176.  
    177. Num_Sommet=0;
    178. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    179. {
    180. QPoint point = QPoint( (((T_Coordonnees[Num_Sommet][0])+15)), ((T_Coordonnees[Num_Sommet][1])+15) );
    181. QString str;
    182. str.setNum(Num_Sommet);
    183. painter.drawText( point,str);
    184.  
    185. }
    186. }
    187.  
    188. ///////FenCC.cpp
    189. #include "FenCC.h"
    190. FenCC::FenCC( QWidget *parent = 0 , int **M) :QDialog(parent)
    191. {
    192. paintEvent(c,M);
    193. fermer = new QPushButton("Fermer",this);
    194. connect(fermer, SIGNAL(clicked()), this, SLOT(accept()));
    195.  
    196. }
    197. void FenCC::paintEvent(QPaintEvent *c ,int **M)
    198. {
    199. QPainter painter(this);
    200. int tour=0;
    201. int x=10;
    202. int y=10;
    203. int Num_Sommet=0;
    204. int T_Coordonnees [10][3];
    205. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    206. {
    207. (T_Coordonnees[Num_Sommet][0])=x;
    208. (T_Coordonnees[Num_Sommet][1])=y;
    209. (T_Coordonnees[Num_Sommet][2])=tour;
    210. if (tour==0)
    211. {
    212. x+=35;
    213. tour=1;
    214. }
    215. else
    216. {
    217. y+=55;
    218. tour=0;
    219. }
    220. }
    221. int w1=0;
    222. int w2=0;
    223. int cpt=10;
    224. Num_Sommet=0;
    225. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    226. {
    227. if ( (T_Coordonnees[Num_Sommet][2])==1)
    228. {
    229. if(w1==0)
    230. {
    231. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+500) ;
    232. w1=1;
    233. }
    234. else
    235. {
    236. if(w1==1)
    237. {
    238. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+250+cpt) ;
    239. w1=0;
    240. cpt=cpt+15;
    241. }
    242. }
    243. if (Num_Sommet==9)
    244. {
    245. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    246. }
    247.  
    248. }
    249. else
    250. {
    251. if(w2==0)
    252. {
    253. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+200;
    254. w2=1;
    255. }
    256. else
    257. {
    258. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+100+cpt;
    259. w2=0;
    260. cpt=cpt+15;
    261. }
    262. if (Num_Sommet==4)
    263. {
    264. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    265. }
    266. }
    267. painter.drawEllipse((T_Coordonnees[Num_Sommet][0]), (T_Coordonnees[Num_Sommet][1]), 30.0, 30.0);
    268. }
    269. int i,j;
    270. for (i=0; i<10; i++)
    271. {
    272. for (j=0; j<10; j++)
    273. {
    274. if (M[i][j]==1)
    275. {
    276. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    277. }
    278. }
    279. }
    280. for (i=0; i<10; i++)
    281. {
    282. for (j=0; j<10; j++)
    283. {
    284. if ((M[i][j]==1) && (M[j][i]==1))
    285. {
    286. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    287. if( (T_Coordonnees[i][1]>45)||(T_Coordonnees[j][1]>45) )
    288. {
    289. painter.drawLine(((T_Coordonnees[i][0]+20)-10),(T_Coordonnees[i][1]),((T_Coordonnees[j][0]+20)-10),(T_Coordonnees[j][1]));
    290. }
    291. else
    292. {
    293. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]+30),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]+30));
    294. }
    295. }
    296. }
    297. }
    298. Num_Sommet=0;
    299. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    300. {
    301. QPoint point = QPoint( (((T_Coordonnees[Num_Sommet][0])+15)), ((T_Coordonnees[Num_Sommet][1])+15) );
    302. QString str;
    303. str.setNum(Num_Sommet);
    304. painter.drawText( point,str);
    305. }
    306. }
    307. ///////////////////main.cpp
    308. #include <QApplication>
    309. #include "FenPrincipale.h"
    310. int main(int argc, char* argv[])
    311. {
    312. QApplication app(argc, argv);
    313. FenPrincipale fenetre;
    314. fenetre.show();
    315. return app.exec();
    316. }
    To copy to clipboard, switch view to plain text mode 
    I respectly repeat that the most important is not this code , but only how to declare the two dimensionned array in the headers and use it in the ".cpp" extensionned files.
    I apreciate the time and attention given to this message.
    Thank you.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TWO DIMENSIONNED ARRAY as an ARGUMENT for all the classes functions

    This is a generic C question. If the dimensions of the array are known and fixed then you simply declare your function:
    Qt Code:
    1. void doStuff(int array[10][10]) { ... }
    2. // and call it
    3. int test[10][10];
    4. doStuff(test);
    To copy to clipboard, switch view to plain text mode 
    If the dimensions are set at run time then you need to allocate the array memory dynamically, pass the dimensions around with the array, and remember to free the memory allocated. You could alternatively also pass a pointer to the first element of the array, along with the dimensions, and access the 1D array element for [r][c] using some simple maths [r*colCount + col]. See Passing multidimensional arrays as function arguments in C

    The generic C++ answer to arrays in general is std::vector. std::vector dispenses with the memory management and array dimension headaches of C. The generic C++ construct for a multi dimensional array is nested std::vectors.
    Qt Code:
    1. #include <iostream>
    2. #include <vector>
    3.  
    4. void dumpVec(const std::vector<std::vector<int> > &vec)
    5. {
    6. for (int r = 0; r < vec.size(); ++r) {
    7. std::cout << r << ":\t";
    8. for (int c = 0; c < vec[r].size(); ++c) {
    9. std::cout << vec[r][c] << "\t";
    10. }
    11. std::cout << std::endl;
    12. }
    13. }
    14.  
    15. int main(int argc, char **argv)
    16. {
    17. static const int rowCount = 10;
    18. static const int colCount = 10;
    19.  
    20. // Declare and init to zero
    21. std::vector<std::vector<int> > vec(rowCount, std::vector<int>(colCount, 0));
    22.  
    23. // simple multiplication table
    24. for (int r = 0; r < rowCount; ++r) {
    25. for (int c = 0; c < colCount; ++c) {
    26. vec[r][c] = r * c;
    27. }
    28. }
    29.  
    30. dumpVec(vec);
    31.  
    32. return 0;
    33. }
    To copy to clipboard, switch view to plain text mode 
    The syntax can be tidied up a bit with typedefs.

    Qt has QVector and QGenericMatrix which may be of use to you.

  3. #3
    Join Date
    Feb 2012
    Posts
    24
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: TWO DIMENSIONNED ARRAY as an ARGUMENT for all the classes functions

    thank you for your answer,
    I tried what you have suggested but without success, can you please see if the problem is in thee declaration in the HEADERS, I really tries all kinds of declarations but it is till not working .The error is that the second parameter of the constructor of FenCC is missing in other terms the array M , I do not understand.So please can you give me any advice concerning the headers evend the declaration of the array M in the private zone of FenPrincipale , I do not know if it is right
    Here is the code:
    Qt Code:
    1. //Headers
    2. #ifndef FENCC_H
    3. #define FENCC_H
    4. #include <QtGui>
    5. class FenCC : public QDialog
    6. {
    7. public:
    8. FenCC ( QWidget *parent , int [10][10]);
    9. private:
    10. QPushButton *fermer;
    11. QPainter *painter;
    12. private slots:
    13. void paintEvent (QPaintEvent *, int [10][10]);
    14. };
    15. #endif
    16.  
    17.  
    18.  
    19. #ifndef FENPRINCIPALE_H
    20. #define FENPRINCIPALE_H
    21. #include <QtGui>
    22. class FenPrincipale : public QWidget
    23. {
    24. Q_OBJECT
    25. public:
    26. FenPrincipale();
    27.  
    28. private slots:
    29. void genererCC(int [10][10]);
    30. void paintEvent(QPaintEvent *,int [10][10]);
    31.  
    32. private:
    33. QPainter *painter;
    34. int M[10][10] ;
    35. QPushButton *generer;
    36.  
    37. };
    38. #endif
    39.  
    40.  
    41.  
    42. //////FenCC.cpp
    43. #include "FenCC.h"
    44. FenCC::FenCC( QWidget *parent = 0 , int M[10][10]) :QDialog(parent)
    45. {
    46. paintEvent(c,M);
    47. fermer = new QPushButton("Fermer",this);
    48. connect(fermer, SIGNAL(clicked()), this, SLOT(accept()));
    49.  
    50. }
    51. void FenCC::paintEvent(QPaintEvent *c ,int M[10][10])
    52. {
    53. QPainter painter(this);
    54. int tour=0;
    55. int x=10;
    56. int y=10;
    57. int Num_Sommet=0;
    58. int T_Coordonnees [10][3];
    59. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    60. {
    61. (T_Coordonnees[Num_Sommet][0])=x;
    62. (T_Coordonnees[Num_Sommet][1])=y;
    63. (T_Coordonnees[Num_Sommet][2])=tour;
    64. if (tour==0)
    65. {
    66. x+=35;
    67. tour=1;
    68. }
    69. else
    70. {
    71. y+=55;
    72. tour=0;
    73. }
    74. }
    75. int w1=0;
    76. int w2=0;
    77. int cpt=10;
    78. Num_Sommet=0;
    79. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    80. {
    81. if ( (T_Coordonnees[Num_Sommet][2])==1)
    82. {
    83. if(w1==0)
    84. {
    85. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+500) ;
    86. w1=1;
    87. }
    88. else
    89. {
    90. if(w1==1)
    91. {
    92. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+250+cpt) ;
    93. w1=0;
    94. cpt=cpt+15;
    95. }
    96. }
    97. if (Num_Sommet==9)
    98. {
    99. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    100. }
    101.  
    102. }
    103. else
    104. {
    105. if(w2==0)
    106. {
    107. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+200;
    108. w2=1;
    109. }
    110. else
    111. {
    112. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+100+cpt;
    113. w2=0;
    114. cpt=cpt+15;
    115. }
    116. if (Num_Sommet==4)
    117. {
    118. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    119. }
    120. }
    121. painter.drawEllipse((T_Coordonnees[Num_Sommet][0]), (T_Coordonnees[Num_Sommet][1]), 30.0, 30.0);
    122. }
    123. int i,j;
    124. for (i=0; i<10; i++)
    125. {
    126. for (j=0; j<10; j++)
    127. {
    128. if (M[i][j]==1)
    129. {
    130. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    131. }
    132. }
    133. }
    134. for (i=0; i<10; i++)
    135. {
    136. for (j=0; j<10; j++)
    137. {
    138. if ((M[i][j]==1) && (M[j][i]==1))
    139. {
    140. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    141. if( (T_Coordonnees[i][1]>45)||(T_Coordonnees[j][1]>45) )
    142. {
    143. painter.drawLine(((T_Coordonnees[i][0]+20)-10),(T_Coordonnees[i][1]),((T_Coordonnees[j][0]+20)-10),(T_Coordonnees[j][1]));
    144. }
    145. else
    146. {
    147. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]+30),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]+30));
    148. }
    149. }
    150. }
    151. }
    152. Num_Sommet=0;
    153. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    154. {
    155. QPoint point = QPoint( (((T_Coordonnees[Num_Sommet][0])+15)), ((T_Coordonnees[Num_Sommet][1])+15) );
    156. QString str;
    157. str.setNum(Num_Sommet);
    158. painter.drawText( point,str);
    159. }
    160. }
    161.  
    162. ///////FenPrincipale.cpp
    163. #include "FenPrincipale.h"
    164. #include "FenCC.h"
    165. FenPrincipale::FenPrincipale()
    166. { int o,p;
    167. int M [10][10];
    168. for (o=0; o<10; o++)
    169. {
    170.  
    171. for (p=0; p<10; p++)
    172. {
    173. M[o][p]=0;
    174. }
    175. }
    176. M[0][3]=1;
    177. M[0][9]=1;
    178. M[2][6]=1;
    179. M[3][5]=1;
    180. M[5][3]=1;
    181. M[2][4]=1;
    182. M[4][2]=1;
    183. paintEvent(e,M);
    184. generer = new QPushButton("&Générer !",this);
    185. connect(generer, SIGNAL(clicked()), this, SLOT(genererCC(M)));
    186. }
    187. void FenPrincipale::genererCC(int M[10][10])
    188. {
    189. FenCC *fenetreCC = new FenCC( this , M);
    190. fenetreCC->exec();
    191. }
    192. void FenPrincipale::paintEvent(QPaintEvent *e, int M[10][10])
    193. {
    194. QPainter painter(this);
    195. int tour=0;
    196. int x=10;
    197. int y=10;
    198. int Num_Sommet=0;
    199. int T_Coordonnees [10][3];
    200. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    201. {
    202. (T_Coordonnees[Num_Sommet][0])=x;
    203. (T_Coordonnees[Num_Sommet][1])=y;
    204. (T_Coordonnees[Num_Sommet][2])=tour;
    205. if (tour==0)
    206. {
    207. x+=35;
    208. tour=1;
    209. }
    210. else
    211. {
    212. y+=55;
    213. tour=0;
    214. }
    215. }
    216. int w1=0;
    217. int w2=0;
    218. int cpt=10;
    219. Num_Sommet=0;
    220. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    221. {
    222. if ((T_Coordonnees[Num_Sommet][2])==1)
    223. {
    224. if(w1==0)
    225. {
    226. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+500) ;
    227. w1=1;
    228. }
    229. else
    230. {
    231. if(w1==1)
    232. {
    233. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+250+cpt) ;
    234. w1=0;
    235. cpt=cpt+15;
    236. }
    237. }
    238. if (Num_Sommet==9)
    239. {
    240. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    241. }
    242.  
    243. }
    244. else
    245. {
    246. if(w2==0)
    247. {
    248. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+200;
    249. w2=1;
    250. }
    251. else
    252. {
    253. (T_Coordonnees[Num_Sommet][0])=(T_Coordonnees[Num_Sommet][0])+100+cpt;
    254. w2=0;
    255. cpt=cpt+15;
    256. }
    257.  
    258. if (Num_Sommet==4)
    259. {
    260. (T_Coordonnees[Num_Sommet][0])=((T_Coordonnees[Num_Sommet][0])+45) ;
    261. }
    262. }
    263. painter.drawEllipse((T_Coordonnees[Num_Sommet][0]), (T_Coordonnees[Num_Sommet][1]), 30.0, 30.0);
    264.  
    265.  
    266. }
    267.  
    268. int i,j;
    269. for (i=0; i<10; i++)
    270. {
    271. for (j=0; j<10; j++)
    272. {
    273. if (M[i][j]==1)
    274. {
    275. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    276. }
    277. }
    278. }
    279.  
    280.  
    281.  
    282. for (i=0; i<10; i++)
    283. {
    284. for (j=0; j<10; j++)
    285. {
    286. if ((M[i][j]==1) && (M[j][i]==1))
    287. {
    288. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]));
    289. if( (T_Coordonnees[i][1]>45)||(T_Coordonnees[j][1]>45) )
    290. {
    291. painter.drawLine(((T_Coordonnees[i][0]+20)-10),(T_Coordonnees[i][1]),((T_Coordonnees[j][0]+20)-10),(T_Coordonnees[j][1]));
    292. }
    293. else
    294. {
    295. painter.drawLine((T_Coordonnees[i][0]+20),(T_Coordonnees[i][1]+30),(T_Coordonnees[j][0]+20),(T_Coordonnees[j][1]+30));
    296. }
    297. }
    298. }
    299. }
    300.  
    301.  
    302.  
    303. Num_Sommet=0;
    304. for (Num_Sommet=0; Num_Sommet<10; Num_Sommet++)
    305. {
    306. QPoint point = QPoint( (((T_Coordonnees[Num_Sommet][0])+15)), ((T_Coordonnees[Num_Sommet][1])+15) );
    307. QString str;
    308. str.setNum(Num_Sommet);
    309. painter.drawText( point,str);
    310.  
    311. }
    312. }
    313.  
    314.  
    315. //////////////main.cpp
    316.  
    317. #include <QApplication>
    318. #include "FenPrincipale.h"
    319. int main(int argc, char* argv[])
    320. {
    321. QApplication app(argc, argv);
    322. FenPrincipale fenetre;
    323. fenetre.show();
    324. return app.exec();
    325. }
    To copy to clipboard, switch view to plain text mode 
    Thank you in advance.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TWO DIMENSIONNED ARRAY as an ARGUMENT for all the classes functions

    Hmm... do you expect your paintEvent() implementation to ever be called (apart from calling it from the constructor yourself which doesn't make any sense)? Furthermore calculating so many things in a painting routine will make your application very slow. As for your current problem, why don't you use more high-level solutions such QGenericMatrix<10,10,int> ?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TWO DIMENSIONNED ARRAY as an ARGUMENT for all the classes functions

    The array M declared at line 34 of your last post listing, and the array M you declare locally in FenPrincipale::FenPrincipale() (line 168) are not the same variable. You initialise the local variable which goes out of scope at the end of the constructor. The member variable is not initialised.

    As for the places you are passing M as a parameter... you are passing a copy of the array. You should consider passing a const reference to the array, or not passing the array at all where it is a member variable.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TWO DIMENSIONNED ARRAY as an ARGUMENT for all the classes functions

    Quote Originally Posted by ChrisW67 View Post
    you are passing a copy of the array. You should consider passing a const reference to the array
    I don't think this matters here since in the end int[][] is really a int** so it is a pointer that is copied, not the contents of the array.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TWO DIMENSIONNED ARRAY as an ARGUMENT for all the classes functions

    Your point about the copy is correct. My mistake.

    However, int[10][10] is 100 ints in a contiguous block of memory (that the compiler accesses using an int* and known geometry), and not a pointer to a block of 10 pointers to blocks of 10 ints each (i.e. not int**). So:
    Qt Code:
    1. void foo(int M[10][10]) { }
    2. void bar(int **M) { }
    3.  
    4. int array[10][10]; // sizeof(array) == 400
    5.  
    6. foo(array); // will compile
    7. bar(array); // error: cannot convert ‘int (*)[10]’ to ‘int**’ for argument ‘1’ to ‘void bar(int**)’
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 5
    Last Post: 17th November 2010, 17:29
  2. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24
  3. Question about functions in classes
    By cwnelatury in forum Newbie
    Replies: 1
    Last Post: 13th May 2009, 06:05
  4. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46
  5. array of pointers to functions
    By moowy in forum General Programming
    Replies: 3
    Last Post: 19th October 2006, 10:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.