I need to make a program to add two matrices but it has to show the usage of objects and classes. The m1.add(); line isn't working. I get error c2228 - left of .add must have class/struct/union
The basic idea of this program is that there is a class called matrix which contains the add() function which takes two 2-dimensional arrays (two matrices) and adds them together and outputs a final matrix and displays it to the user.
I'm trying to use a constructor in the class but not sure if that's the proper way. Basically the two matrices has to pass from the main function to the class either through the function or constructor as parameters.
Also, am I using too many variable names like a,b,c,d,e,f?
#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;
class matrix{
private:
int d[2][2];
int e[2][2];
int f[2][2];
public:
matrix(int a[2][2], int b[2][2]){
d[2][2] = a[2][2];
e[2][2] = b[2][2];
}
int add(){
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
f[i][j] = d[i][j] + e[i][j];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << f[i][j] << "\t";
}
}
cout << "\n";
}
};
void _tmain(int argc, _TCHAR* argv[])
{
int x[2][2];
int y[2][2];
cout << "Enter numbers for Matrix A:\n";
for (int i = 0; i<2; i++)
for (int j = 0; j<2; j++)
{
cin >> x[i][j];
}
cout << "Enter numbers for Matrix B:\n";
for (int i = 0; i<2; i++)
for (int j = 0; j<2; j++)
{
cin >> y[i][j];
}
matrix m1(int x[2][2], int y[2][2]);
m1.add();
cout << "\n";
system("pause");
}