Hi, I'm making a matrix class and I'm currently trying to overload some operators.

+=

#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>
#include <ostream>
#include <string>
#include <sstream>

class Matrix
{
	Matrix& operator = (const Matrix &m);
	
	Matrix& operator += (const Matrix &m);

public:
	Matrix(int x, int y);
	Matrix(Matrix &m);
	~Matrix();

	void Init();
	void Display() const;

	int get_x() const;
	int get_y() const;
	const int * const get_p() const;
	
private:
	bool isInitialized;

	int x, y;
	int *p; // pointer to the matrix
};

#endif // MATRIX_H
Matrix& Matrix::operator +=(const Matrix &m)
{	
	if(x == m.get_x() && y == m.get_y())
	{
		for(int i = 0; i < x; ++i)
			for(int j = 0; j < y; ++j)
				*(p + (i * y) + j) = *(m.get_p() + (i * y) + j);
	}
	return *this;
}

and in main:

#include <iostream>

#include "matrix.h"

using std::cout;	using std::cin;

int main()
{
	Matrix a(2, 2); a.Init();
	Matrix b(2, 2); b.Init();
	
	b += a;
	b.Display();

	system("pause");
	return 0;
}

and I get:

1>c:\users\user\documents\visual studio 2008\projects\matrices\matrices\main.cpp(12) : error C2248: 'Matrix::operator +=' : cannot access private member declared in class 'Matrix'
1> c:\users\user\documents\visual studio 2008\projects\matrices\matrices\matrix.h(13) : see declaration of 'Matrix::operator +='
1> c:\users\user\documents\visual studio 2008\projects\matrices\matrices\matrix.h(10) : see declaration of 'Matrix'
1>Build log was saved at "file://c:\Users\User\Documents\Visual Studio 2008\Projects\Matrices\Matrices\Debug\BuildLog.htm"

I'm not trying to access a private member, am I? And it should be working as well,because I'm doing it from inside the class. Something else is Wrong I think.

Dani AI

Generated

Short diagnosis: the operators were declared before the public: label, so they are private by default (a class gives members private access unless otherwise specified). That is why MSVC reported C2248 when b += a was used — the call comes from outside the class and cannot reach a private member. pointed this out succinctly and correctly explained the default-access rule. (cppreference.com)

Immediate fixes and cleanups (minimal changes):

  • Move operator+= and operator= (and any constructors intended as public) into the public: section of the class.
  • Make the copy constructor take a const reference: Matrix(const Matrix&).
  • Implement operator+= to perform element-wise addition (if that is the intended semantics) and make operator= do a safe deep copy with a self-assignment check.

Example implementations (place in .cpp; signatures must be public in the header):

Matrix& Matrix::operator+=(const Matrix& rhs) {
    if (x != rhs.x || y != rhs.y) throw std::invalid_argument("size mismatch");
    for (int i = 0, n = x*y; i < n; ++i) p[i] += rhs.p[i];
    return *this;
}

Matrix& Matrix::operator=(const Matrix& rhs) {
    if (this == &rhs) return *this;
    if (x != rhs.x || y != rhs.y) { delete[] p; x = rhs.x; y = rhs.y; p = new int[x*y]; }
    std::copy(rhs.p, rhs.p + x*y, p);
    return *this;
}

Because the class manages a raw pointer, follow the Rule of Three/Five: if a destructor, copy ctor or copy-assignment is user-defined, the others likely need to be too (or adopt RAII). Prefer using std::vector<int> for storage to avoid manual new/delete and to get correct copying by default. (cppreference.com)

Quick checklist to avoid similar errors:

  • Verify where declarations sit relative to public:/private:.
  • Use const Matrix& for copy parameters. (cppreference.com)
  • Prefer std::vector<int> (or smart pointers) for owned storage; this simplifies correctness and maintenance.

Recommended Answers

All 4 Replies

They need to be public.

Why? I'm getting there values from the functions get_x, get_y and get_p.

By default class definition body starts with private zone. So you declare both operator= and operator+= as private members of Matrix (look again at you class Matrix). Well, you can't access them outside Matrix member functions and friends of the Matrix class. But your main function is not a friend of Matrix...

Why do you cry now? ;)

arrgh what a mistake :$

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.