-3

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");
}
NoobCoder
  • 189
  • 1
  • 14

2 Answers2

1

There is a lot to comment on about your code, but most importantly this is how you pass an array to a function:

int someArray[] = {1, 2, 3};

void incrementArrayElements(int *someArray)
{
  someArray[0] += 1;
  someArray[1] += 1;
  someArray[2] += 1;
}

An array is essentially a pointer to the first element in it, so you declare the function parameter as a pointer of your type.

Regarding your code, you're having a class matrix which in the constructor takes 2 rank2 arrays for constructing 2 matrices. This makes the name of your class bad as it doesn't describe what an object of that class really is.

Second, your member function, add, is declared int, and so it's supposed to return an integer. Otherwise, you declare it void which is what you use when the function will not return anything.

Third, it appears you're trying to copy the content of the parameter arrays to your member arrays, but that's not how it's done at all. I highly recommend you read on arrays before you attempt creating anything like this.

Last, after you've read on arrays and any other basics covered in the website that you've missed, check out operator overloading. Learn how overloading operator+ can let you add 2 objects a and b of a class easily and cleanly by simply doing a + b.

There are more problems in your code, really. And the snippet you posted shows you've skipped over very important basics.

Aiman Al-Eryani
  • 709
  • 4
  • 19
0

Here is the problem in your code;

In following line you are passing the argument in incorrect way;

matrix m1(int x[2][2], int y[2][2]);

Instead you need to pass as follows;

matrix m1(x, y);

Also please add the return statement in your Add() method as it returns an integer otherwise it will give error.

Sudipta Kumar Sahoo
  • 1,049
  • 8
  • 16