Welcome Guest.   Make a donation to an author on the site August 28, 2014, 02:14:01 AM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Check out and download the GOE 2007 Freeware Challenge productivity tools.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Error 2061: Friend must be functions or classess...Why??????  (Read 4202 times)
Jesica19872008
Participant
*
Posts: 2

View Profile Give some DonationCredits to this forum member
« on: July 06, 2009, 04:25:56 PM »

I have a "matrix.h" for matrix.cpp


matrix.h
[copy or print]
#ifndef __MATRIX_H__
#define __MATRIX_H__

#include <stdexcept>
#include <cassert>

template <class T>
class Matrix
{
    T*           data;
    unsigned int rows;
    unsigned int cols;

    T* new_copy(const T* src, size_t src_size, size_t dst_size)
    {
        assert(dst_size >= src_size);
        T* dest = new T[dst_size];
        try
        {
            std::copy(src, src + src_size, dest);
        }
        catch(...)
        {
            delete[] dest;
            throw;
        }
        return dest;
    }

public:
    Matrix(unsigned int _rows, unsigned int _cols):
        rows(_rows),
        cols(_cols)
    {
        assert(rows > 0);
        assert(cols > 0);
        if (rows == 0 || cols == 0)
            throw std::invalid_argument("Invalid argument in Matrix ctor");

        data = new T[rows * cols];
        if (!data)
            throw std::runtime_error("Not enough memory in Matrix ctor");
    }

    ~Matrix()
    {
        delete[] data;
    }

    Matrix(const Matrix& m):
        data(new_copy(m.data, m.rows * m.cols, m.rows * m.cols)),
        rows(m.rows),
        cols(m.cols)
    {
    }

    Matrix& operator=(const Matrix& m)
    {
        if (this != &m)
        {
            T* temp = new_copy(m.data, m.rows * m.cols, m.rows * m.cols);
            delete[] data;
            data = temp;
            rows = m.rows;
            cols = m.cols;
        }
        return *this;
    }

    template<class U>
    class Helper
    {
        Matrix<U>&   matrix;
        unsigned int i;

        Helper(Matrix<U>& _mc, unsigned int _i): matrix(_mc), i(_i) {};
        Helper(const Matrix<U>& _mc, unsigned int _i): matrix(const_cast<Matrix&>(_mc)), i(_i) {};
        Helper(const Helper&);
        Helper& operator=(const Helper&);

[b]        friend class Matrix<U>;[/b]

    public:
        U& operator[](unsigned int k) const
        {
            assert(i < matrix.rows);
            assert(k < matrix.cols);
            if (i >= matrix.rows || k >= matrix.cols)
                throw std::range_error("Range error in Matrix::Helper::operator[]() const");

            return matrix.data[matrix.cols * i + k];
        }

        U& operator[](unsigned int k)
        {
            assert(i < matrix.rows);
            assert(k < matrix.cols);
            if (i >= matrix.rows || k >= matrix.cols)
                throw std::range_error("Range error in Matrix::Helper::operator[]()");

            return matrix.data[matrix.cols * i + k];
        }
    };

    Helper<T> operator[](unsigned int i) const
    {
        assert(i < rows);
        if (i >= rows)
            throw std::range_error("Range error in Matrix::operator[]() const");

        return Helper<T>(*this, i);
    }

    Helper<T> operator[](unsigned int i)
    {
        assert(i < rows);
        if (i >= rows)
            throw std::range_error("Range error in Matrix::operator[]()");

        return Helper<T>(*this, i);
    }

};

#endif //__MATRIX_H__



Why in dedicated line Borland C++ Builder 5 give up mistake (E2061 Friends must be function or classes)

or

how this part of code must be make over for successfull compile Borland C++ Builder?
p.s.: MS Visual C++ 2005 compile successfully

...please...help!!!
Logged
mouser
First Author
Administrator
*****
Posts: 33,279



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #1 on: July 06, 2009, 04:48:01 PM »

youve got nested templated classes
   "template<class U> class Helper" is inside "template <class T> class Matrix"

while the code may be ok, i wouldnt be surprised if the old Borland C++ Builder 5 compiler had a bug and was getting confused with this.  im not sure what a workaround would be..

though i could be wrong.
Logged
Jesica19872008
Participant
*
Posts: 2

View Profile Give some DonationCredits to this forum member
« Reply #2 on: July 06, 2009, 04:57:21 PM »

ohh...not only Borland C++ Builder give out this mistake.  CodeGear too  Sad

May be you have any ideas to get clear of this mistake?
Logged
wraith808
Supporting Member
**
Posts: 6,207



"In my dreams, I always do it right."

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #3 on: July 07, 2009, 07:16:31 AM »

Codegear is (was) Borland- now Embarcadero, so it would be the same problem.  I'd suggest asking on the CodeGear site if you can't get any help here.
Logged

Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.033s | Server load: 0.04 ]