I am getting this error when I compile. I have tried taking Add out the code, but I do not get an output then. How can I fix this to produce an output??

//This program serves as a test for the Matrix class

#include <iostream>
#include "Matrix.h"

using namespace std;

int main (int argc, char * const argv[]) 
{
	
	Matrix mat1;
	Matrix mat2;
	int rows;
	int cols;
	int initValue;
	

	cout << "Please enter the number of Rows:  " << "\n";
	cin >> rows;
	cout << "Please enter the number of Columns:  " << "\n";
	cin >> cols;
	cout << "Enter the initial value for the Matrix:  " << "\n";
	cin >> initValue;
	Matrix(rows, cols, initValue);
	
	for(int i=0; i < rows; i++)
		for(int j=0; j < cols; j++)
			Add(mat1, mat2);
	
	for(int i=0; i < rows; i++)
		for(int j=0; j < cols; j++)
			Multiply(mat1, mat2);

	return 0;
}
/*
 *  Matrix.h
 *  Matrix
 *
 *  Created by Dawn Ellis on 8/14/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>
#include <vector>
using namespace std;

class Matrix
{
private:
	int numberOfRows;
	int numberOfColumns;
	vector<vector<int>> mat;

	public:
		/*	Default constructor
			Creates a 1x1 matrix that contains 0 */
		Matrix();
	

		/*	Parameterized constructor
			Create a matrix of the specified size with all cells set to the 
			initial value.
			Inputs: three integer values, the first two represent the number
			of rows and columns in the matrix, and the third integer
			represents the initial value of the matrix cells. */
		Matrix(int rows, int cols, int initValue);

		/*	Implements matrix addition.
			Inputs:	A matrix to add to the current matrix
			Outputs: A new matrix which is the result of adding two matrices together */
		void Add(vector<vector<int> >mat1, vector<vector<int> >mat2);

		/*	Implements matrix subtraction.
			Inputs: the matrix to multiply from the current matrix.
			Outputs: A new matrix which is the result of the difference between the
			two matrices. */
		void Multiply(vector<vector<int> >mat1, vector<vector<int> >mat2);


};
#endif
#include <iostream>
#include "Matrix.h"

using namespace std;

Matrix::Matrix()
{
	int numberOfRows = 1;
	int numberOfColumns = 1;

	vector<int> mat(numberOfRows, 0);

	vector<vector<int> > mat2d1(numberOfColumns,mat); 

	for(int i=0; i < mat2d1.size(); i++)
		for (int j=0; j < mat2d1[i].size(); j++) 
			cout << mat2d1[i][j] << " "; cout << endl;
	
}
Matrix::Matrix(int rows, int cols, int initValue)
{
	int numberOfRows = rows;
	int numberOfColumns = cols;

	vector<int> mat(numberOfRows, initValue);

	vector<vector<int> > mat2d1(numberOfColumns,mat); 

	for(int i=0; i < mat2d1.size(); i++)
		for (int j=0; j < mat2d1[i].size(); j++) 
			cout << mat2d1[i][j] << " " << endl;
	
}

void Matrix::Add(vector<vector<int> >mat1, vector<vector<int> >mat2)
{
	for (int i=0; i < numberOfRows; i++)
	{
		for (int j=0; j < numberOfColumns; j++)
			mat1[i][j]+=mat2[i][j];}	
			for (int i=0; i < numberOfRows; i++)
	{
		for (int j=0; j < numberOfColumns; j++)
			cout << mat1[i][j];}
		

}


void Matrix::Multiply(vector<vector<int> >mat1, vector<vector<int> >mat2)
{	
	for (int i=0; i < numberOfRows; i++)
	{
		for (int j=0; j < numberOfColumns; j++)
			mat1[i][j]*=mat2[i][j];}
	for (int i=0; i < numberOfRows; i++)
	{
		for (int j=0; j < numberOfColumns; j++)
			cout << mat1[i][j];}

}

Error 1 error C3861: 'Add': identifier not found e:\312 2\vectormatrix\vectormatrix\main.cpp 28

on lines 28 and 32 of the Main file.
Sorry forgot to post that. Ty

In main() you aren't instantiating your matrix class correctly.
Also your add and multiply functions are currently member functions so you'd have to access them via an instance of your Matrix class.

To correctly instantiate your class you need to do something like:

Matrix* mat1 = new Matrix(rows, cols, initValue); // pointer to a new matrix

or perhaps:

Matrix mat1(rows, cols, initValue);

Likewise, you'd have to do something similar to instantiate mat2.

Then there's this bit of code in main, which is what is causing the errors you mentioned:

for(int i=0; i < rows; i++)
		for(int j=0; j < cols; j++)
			Add(mat1, mat2);
	
	for(int i=0; i < rows; i++)
		for(int j=0; j < cols; j++)
			Multiply(mat1, mat2);

As mentioned, the problem here is that you are trying to access a function which is a member of a class, but you don't have a proper instance of the Matrix class...

I'm not going to solve the whole problem here for you, but I can point you in the right direction...
What you really need to be doing is something like this in main:

Matrix* resultOfAddition = mat1->Add(mat2);
Matrix* resultOfMultiplication = mat1->Multiply(mat2);

or:

Matrix* resultOfAddition = mat1.Add(mat2);
Matrix* resultOfMultiplication = mat1.Multiply(mat2);

(depending on how you instantiated your class!)


You then need to change your Add and Multiply functions, so they take a constant reference or a pointer to a Matrix object as a parameter also make them return a pointer to a new Matrix object...

So all you do is inside the function, first check that the matrices are conformable (if applicable!), then create a new Matrix called result.
You then add/multiply the current instances data with the data in the passed in matrix and store the result in the result matrix.
At the end of the calculation process return the result matrix.
So here's the definition of the Matrix::Multiply() function in pseudo code

Matrix* Matrix::Multiply(const Matrix& mat2)
{
    //1. Check matrices are conformable
    // i.e. this.numberofRows==mat2.GetNumberOfRows() 
    // don't forget to check the number of cols.
    // HINT: you'll need to add some extra public accessor methods to your Matrix Class!

    // if not conformable return null;
    // else create a pointer to new matrix called result.

    // now multiply the contents of 'this' matrix with the contents of mat2 and store the results in result.

   // now return result (as a pointer!)
}

I think that's about all you need to know for now.
Give that a shot and let me know how you get on!

Another thing you could do is add another public member function to Matrix to display the data in the Matrix using cout. (perhaps something like Matrix:: DisplayData ??).
Then in main you could call the function to display the results of your operation. i.e. after performing the addition and multiplication...

cout << "Result of adding the two matrices:"<<endl; 
resultOfAddition->DisplayData();

Anyways, that's my two cents!
Cheers for now,
Jas.

Edited 7 Years Ago by JasonHippy: typos..as usual

Comments
Much more detailed than my terse effort

They're class member functions, which means they need an object to be applied to.

Eg. mat1.Add(mat1, mat2); Of course, you might regard one of your parameters as now being redundant.

Also, I don't think your initial constructor call is very good either. What is it constructing?

This article has been dead for over six months. Start a new discussion instead.