Visual C++ and friend functions giving errors

This is a discussion on Visual C++ and friend functions giving errors within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem with a friend fucntion that overloads operator << to print my class. This is pretty simple ...

  1. #1
    Unregistered
    Guest

    Visual C++ and friend functions giving errors

    I'm having a problem with a friend fucntion that overloads operator << to print my class. This is pretty simple and I've modeled my function after the Dietel book. My other functions work.

    My class:

    class DynamicArray{

    friend ostream& operator<<(ostream&,const DynamicArray& );


    public:

    ~DynamicArray();
    DynamicArray(int size = 1, int value = 0);
    DynamicArray (int array [], int size = 1);
    DynamicArray (const DynamicArray &);
    DynamicArray& operator=(const DynamicArray &);
    int& operator [] (int index);
    int GetFront (void) const;
    int GetBack (void) const;
    void InsertAtIndex (int value, int index); (other functions)
    private:

    int size;
    int capacity;
    int * ptr;
    };


    And then I wrote this to print my class. Linux doesn't give me any problems but this pukes with Visual C++.


    ostream & operator<<( ostream& output, const DynamicArray& rhs)
    {
    //test for empty array
    if(rhs.size == 0)
    {
    output << "DynamicArray is empty" << endl;
    return output;
    }

    //otherwise print out the array as "output"
    for(int i = 0; i < rhs.size; i++)
    {
    output << rhs.ptr[i] << ",";
    }

    output << endl;

    return output;
    }

    Can anyone spot what the problem is?

    Thanks,
    Robert

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What error messages?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest
    The error message is: 'size' cannot access private member declared in class DynamicArray.
    This error message is put out each time I try to access a private member in the friend function.

    Thanks,
    Robert

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    There was a bug with MSVC along these lines, that was fixed with one of the service packs.
    zen

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I dont know whether its a typo on your part but operator << is defined as private in your example and it should be in the public section.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21