Note that in those assignments and cout statements, you don't actually call the operator[]( unsignedint ) of Fraction, you are simply doing the old way of accessing
elements in an array.
The problem, maybe is you don't have an overloaded operator<< for fraction instead, so : cout << "[]: f[0] = " << f[0] << ". f[1] = " << f[1] << endl;
will not work.
#include <iostream>
class Fraction
{
public :
Fraction( int x = 0, int y = 0 ) : x_(x), y_(y) { }
// declare as friend to be able to access private members of the class
friend std::ostream& operator << ( std::ostream& stream, const Fraction& fraction );
private :
int x_, y_;
};
// NOTE :
// operator << is not a member function, it is simply a function that has access to private members
// of Fraction
std::ostream& operator << ( std::ostream& stream, const Fraction& fraction )
{ return stream << fraction.x_ << '/' << fraction.y_; } // accesing private member of fraction
int main ()
{
Fraction fracs[ 4 ] { {3, 4}, {1, 2}, {9, 3}, {68, 37} };
for( int i = 0; i < 4; ++i )
std::cout << fracs[ i ] << std::endl; // using operator << we have defined
}