Dynamic Array of Objects

I'm working with a simple object "Fraction." It holds a numerator and a denominator. I've also make a member function called .print() that simply prints the fraction.

When I try to use the .print() on a member of a dynamic array I get an error when I try to compile.

Isn't a pointer to an object usable as an object and can use the dot (.) modifier to call member functions?

This is the error I get when I try to compile.

1
2
3
$g++ test.cpp 
test.cpp: In function ‘int main()’:
test.cpp:66: error: request for member ‘print’ in ‘(frPtr + 16u)’, which is of non-class type ‘Fraction*’


Here is the code below

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
// playing with pointers and objects

#include <iostream>
using namespace std;

class Fraction {
public:

   Fraction( void ) {  // default constructor

      num = 1;
      denom = 1;
}

   Fraction( int arg1, int arg2 ) {  // set 

      num = arg1;
      denom = arg2;

}

   ~Fraction( void ) {  // deconstructor

}

   void print( void );  // prints the fraction

private:
   int num;
   int denom;

}; // end Fraction class

void loadFractions( Fraction*, int );
  // loads the dynamic array with fractions 


int main( void ) {

   int size;

   cout << "How many fractions would you like to work with : ";
   cin >> size;

   // setting up a dynamic array of fractions. 
   Fraction* frPtr = new Fraction[ size ];

   loadFractions( frPtr, size );

   // testing .print() on a simple fraction
   Fraction x( 2, 3 );
   cout << "Value of x.print() " << endl;
   x.print();

   // setting up a normal array of Fractions. 
   Fraction frArray[ 2 ] = { Fraction( 1, 2 ), Fraction( 2, 3 ) };

   // this works!
   cout << "Value of frArray[ 0 ].print " << endl;
   frArray[ 0 ].print();

   
   // assuming I have a dynamic array of at lest size 3
   // attempting to use .print() to print the 2nd member of 
   // frPtr.  Doesn't work. 
   *( frPtr + 2 ).print();

   return 0;

}

void loadFractions( Fraction* arg, int size ) {

   int inputNum;
   int inputDenom;

   for ( int i = 0; i < size; i++ ) {

      cout << "Element [ " << i << " ] "
           << "\n num   : ";
      cin >> inputNum;
      cout << " Denom : ";
      cin >> inputDenom;

      *( arg + i ) = Fraction( inputNum, inputDenom );

   }

   return;

}
 
void Fraction::print( void ) {

   cout << "( " << num << " / " << denom << " ) " << endl;

   return;

}


What I'd like to be able to is send the dynamic array though a for loop and print them all out.
Last edited on
You should consider using the -> operator when working with pointers, rather than the . operator. It makes the intention a little more clear. But I see you are trying to dereference the pointer before using the . operator. That would work, but operator precedence gets in the way. The . operator takes precedence here. The compiler is interpreting this as:

*(( frPtr + 2 ).print());

What you intended was:

(*( frPtr + 2 )).print();

See this for more information: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

Additionally, the second element is not frPtr + 2, but fpPtr + 1.
Ahhhh I see. I didn't even think about that. Thank you so much! Also

 
( frPtr + 2 )->print();


works great and it looks better! I tried -> before but I was doing the syntax wrong.

Now I'll just throw that baby into a for loop and we'll be good to go! Thank you very much!! :D
Topic archived. No new replies allowed.