Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange mistake:: E2061 Friends must be function or classes

Status
Not open for further replies.

wwef

Instructor
Jul 6, 2009
1
RU
I have a "matrix.h" for matrix.cpp


matrix.h
Code:
#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!!!
 
I copy pasted into my C++Builder 2007 pro and it received no errors compiling this header file.
 
I also copy paste the code in C++Builder 5. And again I didn't receive any error. I forced an error E2061 by placing the line "friend class Matrix<U>;" outside the class and I got the error E2061.

In your place I should look for an forgotten "}" elsewhere, maybe in a cpp-file. This code I think has no mistake that could cause an E2061 error.
 
Don't forget that C++Builder 5 has a bug, especially when you moved your files to an other location.

The work around is:
Open a new project and import all your files into this new project.
Compile your project.

I hope you got trough this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top