Hello, I am having an issue with C++. I am relatively new to this language.

The issue I am having is that I am unable to use a variable declared as private in my class.

Definition:

class Matrix 
{
	public:
		Matrix(void);
		Matrix(int rows, int cols);
		Matrix(const Matrix& m);
		~Matrix(void);

		float GetM(int row, int col) const;
		void SetM(int row, int col, float data);

		Matrix& operator=(const Matrix &rhs);
		Matrix operator*(const Matrix &matrix2);

		int NumRows() const;
		int NumCols() const;

		void Resize(int rows, int cols);

	private:
		vector<vector<float>> _matrix;
		void Copy(const Matrix& m);
};

When I try to access _matrix in the operator* function as follows:

Matrix Matrix::operator* (const Matrix &matrix2) 
{
	int i, j;
	float sum;
	Matrix result;

    int i, j, k;
    float sum;
    for (i = 0; i < _matrix.NumRows(); i++) {
		for (j = 0; j < matrix2.NumCols(); j++) {
        sum = 0;
        for (k = 0; _matrix.NumCols(); k++) {
            sum += _matrix.GetM(i, k) * matrix2.GetM(k, j);
                    }
            result.SetM(i, j, sum);
            }
    }
	return result;
}

I get the error message:
IntelliSense: class "std::vector<std::vector<float, std::allocator<float>>, std::allocator<std::vector<float, std::allocator<float>>>>" has no member "GetM".

However, I am able to use _matrix in other classes without an issue:

float Matrix::GetM(int a, int b) const
{
	return _matrix[a][b];
}

void Matrix::SetM(int a, int b, float x)
{
	_matrix[a][b] = x;
}

void Matrix::Resize(int rows, int cols) 
{
	_matrix.resize(rows);
	for(int i = 0; i < rows; i++)
	{
		_matrix[i].resize(cols);
	}
}

Hopefully someonce can point out what I am doing wrong. I have tried searching Google for similar errors, but I am unable to find anything. maybe I am using the wrong search criteria.. Any help would be appreciated.

The problem is line 13

_matrix.GetM(i, k)

The vector container has no method called GetM().

The error message and gerard4143 say it.
I think you are confusing std::vector<> and Matrix.

Hello, I am having an issue with C++. I am relatively new to this language.

The issue I am having is that I am unable to use a variable declared as private in my class.

Definition:

class Matrix 
{
	public:
		Matrix(void);
		Matrix(int rows, int cols);
		Matrix(const Matrix& m);
		~Matrix(void);

		float GetM(int row, int col) const;
		void SetM(int row, int col, float data);

		Matrix& operator=(const Matrix &rhs);
		Matrix operator*(const Matrix &matrix2);

		int NumRows() const;
		int NumCols() const;

		void Resize(int rows, int cols);

	private:
		vector<vector<float>> _matrix;
		void Copy(const Matrix& m);
};

When I try to access _matrix in the operator* function as follows:

Matrix Matrix::operator* (const Matrix &matrix2) 
{
	int i, j;
	float sum;
	Matrix result;

    int i, j, k;
    float sum;
    for (i = 0; i < _matrix.NumRows(); i++) {
		for (j = 0; j < matrix2.NumCols(); j++) {
        sum = 0;
        for (k = 0; _matrix.NumCols(); k++) {
            sum += _matrix.GetM(i, k) * matrix2.GetM(k, j);
                    }
            result.SetM(i, j, sum);
            }
    }
	return result;
}

I get the error message:
IntelliSense: class "std::vector<std::vector<float, std::allocator<float>>, std::allocator<std::vector<float, std::allocator<float>>>>" has no member "GetM".

However, I am able to use _matrix in other classes without an issue:

float Matrix::GetM(int a, int b) const
{
	return _matrix[a][b];
}

void Matrix::SetM(int a, int b, float x)
{
	_matrix[a][b] = x;
}

void Matrix::Resize(int rows, int cols) 
{
	_matrix.resize(rows);
	for(int i = 0; i < rows; i++)
	{
		_matrix[i].resize(cols);
	}
}

Hopefully someonce can point out what I am doing wrong. I have tried searching Google for similar errors, but I am unable to find anything. maybe I am using the wrong search criteria.. Any help would be appreciated.

What he meant to do (I believe) is:

sum += GetM(i, k) * matrix2.GetM(k, j);

What he meant to do (I believe) is:

sum += GetM(i, k) * matrix2.GetM(k, j);

I'll try that and see if it works.

Care to explain why I cannot use _matrix in this case when I can in the other functions I have?

Hey Will.
Gerard, Caligula... and Greywolf are correct.

In order to understand why your code is not working, take another look at the definition of _matrix in your code.

_matrix is declared as std::vector<std::vector<float>> . So it's a std::vector, NOT an instance of your Matrix class.

So this code: _matrix.GetM(i,k) is failing because functions like GetM() , NumRows() and NumCols() which you're using in the operator* function are all member functions of your Matrix class, NOT member functions of _matrix, which is a std::vector.

Hope that clarifies things for you!

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.