Форум С++

 

Ответить на сообщение

Вернуться к теме

Вы отвечаете на сообщение:

Автор: AleksandraMV   (06.03.2009 в 09:03)
Добрый день!
есть код, на который компилятор билдера 5го ругается
на выделеную строку в коде выдает ошибку(хотя вроде корректно написано :()
error---E2061 Friends must be functions or classes

вот код
#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&);

friend class Matrix<U>;

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);
}

};


Ваше имя:

Пароль:

Цитировать

Используйте тэги для выделения текста:
Код: [code][/code]
Жирный: [b][/b]
Наклонный: [i][/i]
URL: [url][/url]

Сообщение:

Прикрепить: