Технический форум
Вернуться   Технический форум > Программирование > Форум программистов > Помощь студентам


Ответ
 
Опции темы Опции просмотра
Старый 20.01.2012, 15:43   #1 (permalink)
Genius108
Новичок
 
Регистрация: 20.01.2012
Сообщений: 3
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 10
По умолчанию Перегрузка операции индексации

Необходимо перегрузить оператор индексации [ ] , я сделал как мог, но преподу не понравилось, сказал перегрузить с помощью вспомогательного класса. Я не знаю как, помогите, пожалуйста.

Вот мой код:
Код:
#pragma once
#include <iostream>
using namespace std;

class matrix
{
	private:
		
		double *a;
		int n, m;

	public:

		matrix (int = 0,int = 0, double* = NULL);
		matrix (const matrix&);
		~ matrix ();

		bool umnojenie (const matrix& ) const;
		bool summa (const matrix& ) const;

		const double* operator[] (int i) const;
		double* operator[] (int i);

		matrix& operator = (const matrix&);
		matrix& operator +=(const matrix&);
		matrix& operator -=(const matrix&);
		matrix& operator *=(const matrix&);
		matrix& operator *=(const double);

		friend matrix operator * (const matrix&, const matrix&);
		friend matrix operator * (const matrix&, double);
		friend matrix operator * (double , const matrix&);
		friend matrix operator + (const matrix&, const matrix&);
		friend matrix operator - (const matrix&, const matrix&);

		int get_Row () const;
		int get_Col () const;
	
		friend ostream& operator << (ostream&, const matrix &);
};
Genius108 вне форума   Ответить с цитированием

Старый 20.01.2012, 15:43
Helpmaster
Member
 
Аватар для Helpmaster
 
Регистрация: 08.03.2016
Сообщений: 0

Проблема частая, на форуме уже создавались схожие обсуждения

Операции с файлом
Убираем «Ошибка при выполнении операции» в Java на SE
Арифметические операции в двоичной системе счисления
Перегрузка операторов комплексных чисел в С++
Перегрузка процессора

Старый 20.01.2012, 15:47   #2 (permalink)
Genius108
Новичок
 
Регистрация: 20.01.2012
Сообщений: 3
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 10
По умолчанию

P.S. Компилятор Visual C++
Genius108 вне форума   Ответить с цитированием
Старый 26.01.2012, 16:30   #3 (permalink)
Genius108
Новичок
 
Регистрация: 20.01.2012
Сообщений: 3
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 10
По умолчанию

попытался сделать так, но как-то косячно все это
Код:
#pragma once
#include <iostream>
using namespace std;

class matrix
{
	private:
	
		double *a;
		int n, m;
class Row
	{
	private:
		matrix* _matrix;
		int row;
		int i, m, j;

	public:

		double operator[](const int col)
		{
			if( i<0 || i>=m ) throw "Ошибка";
	
			return _matrix->GetValue(row,col);
		}
		Row(matrix* _m,int row)
		{
			_matrix=_m;
			this->row=row;

		}
	 };
			
	
 	public:

matrix :: matrix (const matrix &_matrix)
{
	cout << "Конструктор копирования " << this << endl;
	n=_matrix.n;
	m=_matrix.m;
	a = _matrix.a ? new double[n*m] : NULL;
	if(_matrix.a) memcpy(a,_matrix.a,n*m*sizeof(double));
}
	

		matrix (int = 0,int = 0, double* = NULL);
		matrix (const matrix&);
		~ matrix ();

		bool umnojenie (const matrix& ) const;
		bool summa (const matrix& ) const;

	//	const double* operator[] (int i) const;
	    Row operator[](int i) const;
		
		matrix& operator = (const matrix&);
		matrix& operator +=(const matrix&);
		matrix& operator -=(const matrix&);
		matrix& operator *=(const matrix&);
		matrix& operator *=(const double);

		friend matrix operator * (const matrix&, const matrix&);
		friend matrix operator * (const matrix&, double);
		friend matrix operator * (double , const matrix&);
		friend matrix operator + (const matrix&, const matrix&);
		friend matrix operator - (const matrix&, const matrix&);

		int get_Row () const;
		int get_Col () const;
	
		friend ostream& operator << (ostream&, const matrix &);

		int GetValue(int row, int col) const;
};
Genius108 вне форума   Ответить с цитированием
Ads

Яндекс

Member
 
Регистрация: 31.10.2006
Сообщений: 40200
Записей в дневнике: 0
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 55070
Ответ

Опции темы
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Выкл.
HTML код Выкл.
Trackbacks are Вкл.
Pingbacks are Вкл.
Refbacks are Выкл.




Часовой пояс GMT +4, время: 17:08.

Powered by vBulletin® Version 6.2.5.
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.