Calling a member function from another class.

Getting different errors no matter what I seem to try. This is an assignment, I can't use pointers, or statics, or change anything in the public section. In my Flipit::Fetch(int row int col) function, I'm trying to call a function from a function from another class also called fetch that takes the same parameters.
I'm creating the Grid object in my constructor, and trying to use that object in another member function in the flipit class (on line 31). Not sure what I'm doing wrong here. I'm using the GCC compiler (in case Microsoft allows this kind of syntax).

Thanks!

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
#include "grid.h"
#include "flipit.h"
#include <iostream>

using namespace std;

FlipIt::FlipIt( int      nRows,
                int      nCols,
                int      gameNum,
                int      complexity,
                Pattern  pattern,
                bool     wrap ):
                mv_row(nRows) , mv_col(nCols) , mv_gameNum(gameNum) ,
                mv_complex(complexity) , mv_patrn(pattern) ,
                mv_wrap(wrap)
  {
	 Grid g(mv_row , mv_col); //OBJECT CREATED.
  }

  int FlipIt::numRows() const { return mv_row; }

  int FlipIt::numCols() const { return mv_col; }

  void  FlipIt::click( int  row, int  col )
  {

  }

  FlipIt::Color  FlipIt::fetch( int  row, int  col ) const
  {
		return g.fetch(row , col); // TRYING TO CALL OBJECT HERE
  }

  bool  FlipIt::done() const
  {
		return true;
  }


Here is the flipit header

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

#ifndef  flipit_H
#define  flipit_H


#include  "grid.h"


class  FlipIt
{
  public  :

    enum  Color  { clear_ = false, solid_ = true };

    enum  Pattern  { cross_, x_, square_, hollowSquare_, corners_ };

    FlipIt( int      nRows,
            int      nCols,
            int      gameNum,
            int      complexity,
            Pattern  pattern,
            bool     wrap );

    int  numRows()  const;
    int  numCols()  const;

    void  click( int  row, int  col );

    Color  fetch( int  row, int  col ) const;

    bool  done() const;

  private  :

    			int     mv_row;
    			int     mv_col;
    			int     mv_gameNum;
    			int     mv_complex;
    			bool    mv_wrap;
    			Pattern mv_patrn;
};


#endif 


and the Grid header.

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

#ifndef  grid_H
#define  grid_H


class  Grid
{
  public  :

    Grid( int  nRows, int  nCols );
      //
      // Constructor
      //
      // This will use dynamic memory to set up an internal
      // data structure to store the state of all the cells
      // in the grid based on the number of rows and
      // columns provided as parameters. All cells are
      // initialized to the cleared state by this function.
      //


    ~Grid()  { cleanup(); }
      //
      // Destructor
      //
      // Cleans up the dynamic memory used by this class.
      // This is called automatically. Do not make an explicit
      // call to this function.
      //


    int  numRows()  const  {  return  m_numRows;  }
      //
      // Returns the number of rows in the grid.
      //


    int  numCols()  const  {  return  m_numCols;  }
      //
      // Returns the number of columns in the grid.
      //


    void  set( int  row, int  col );
      //
      // Sets a specific cell of the grid. The row and
      // column values are expected to be zero-based.
      //


    void  clear( int  row, int  col );
      //
      // Clears a specific cell of the grid. The row and
      // column values are expected to be zero-based.
      //


    bool  fetch( int  row, int  col )  const;
      //
      // Returns true if the specified cell is set, false
      // if is clear. The row and column values are expected
      // to be zero-based.
      //


    Grid( const Grid  &rhs );
      //
      // Copy Constructor
      //
      // This is needed so that objects of this class type
      // can be copied.
      //


    const Grid  &operator=( const Grid  &rhs );
      //
      // Assignment operator
      //
      // This is needed so that objects of this class type
      // can be assigned.
      //


  private  :

    int  m_numRows;
    int  m_numCols;

    bool  *m_cells;

    int  index( int  row, int  col )  const;

    void  copy( const Grid  &rhs );
    void  cleanup()                   { delete  []  m_cells; }
};


#endif 
Grid g is not a member of FlipIt. In the constructor, you create a Grid g then immediately throw it away. In the private section of class FlipIt you need to declare Grid g; there. Then in the constructor, put g in the initialization list:
1
2
3
4
5
6
7
8
9
10
11
FlipIt::FlipIt( int      nRows,
                int      nCols,
                int      gameNum,
                int      complexity,
                Pattern  pattern,
                bool     wrap ):
                mv_row(nRows) , mv_col(nCols) , mv_gameNum(gameNum) ,
                mv_complex(complexity) , mv_patrn(pattern) ,
                mv_wrap(wrap), g(nRows, nCols)
  {
  }
Last edited on
Thanks! Works perfect!
Topic archived. No new replies allowed.