I have an object which is an array of complex numbers. I wish to create a member function to assign real and imaginary numbers to each element. Whilst I successfully made a constructor to initialise this object to 0.0 fpr all elements, when I make a custom function to assign user input values for the elements I get this issue
no operator "[]" matches these operands
101
Error C2676 binary '[': 'ComplexMatrix' does not define this operator or a conversion to a type acceptable to the predefined operator
101
#pragma once
#include <iostream>
#include <complex>
#include <cmath>
usingnamespace std;
class ComplexMatrix
{
private:
complex<longdouble>** Arr;
int mi = 3;
int mj = 3;
public:
ComplexMatrix();
ComplexMatrix(int i, int j);
void DisplayMatrix();
void DeleteMatrix();
void EnterComplexMatrix(ComplexMatrix&, int, int);
};
//Constructor to initialise a default 3 x 3 complex matrix with 0s for real and imaginary values
ComplexMatrix::ComplexMatrix()
{
Arr = new complex<longdouble> * [mi];
for (int x = 0; x < mi; x++)
{
Arr[x] = new complex<longdouble> [mj];
}
for (int x1 = 0; x1 < mi; x1++)
{
for (int y1 = 0; y1 < mj; y1++)
{
Arr[x1][y1] = (0.0, 0.0);
}
}
}
//Constructor to initialise a user defined complex matrix of a particular size with 0s for real and imaginary values
ComplexMatrix::ComplexMatrix(int i, int j)
{
mi = i;
mj = j;
Arr = new complex<longdouble> * [i];
for (int x = 0; x < i; x++)
{
Arr[x] = new complex<longdouble>[j];
}
for (int x1 = 0; x1 < i; x1++)
{
for (int y1 = 0; y1 < j; y1++)
{
Arr[x1][y1] = (0.0, 0.0);
}
}
}
//Display the matrix member function
void ComplexMatrix::DisplayMatrix()
{
for (int x = 0; x < mi; x++)
{
for (int y = 0; y < mj; y++)
{
cout << Arr[x][y];
}
cout << endl;
}
}
//Delete the memory allocated by the matrix member function
void ComplexMatrix::DeleteMatrix()
{
for (int x = 0; x < mi; x++)
{
delete[] Arr[x];
}
delete[] Arr;
}
//Enter complex matrix elements member function
void ComplexMatrix::EnterComplexMatrix(ComplexMatrix& Arr, int i, int j)
{
double real, img;
complex < longdouble> temp = (0.0, 0.0);
cout << "Your matrix will have " << i * j << " elements" << endl;
//Prompt for user input and assign values for real and imaginary values
for (int x = 0; x < i; x++)
{
for (int y = 0; y < j; y++)
{
cout << "Enter the details for the real part of element" << "[" << x << "]" << "[" << y << "]" << endl;
cin >> real;
cout << "Enter the details for the real part of element" << "[" << x << "]" << "[" << y << "]" << endl;
cin >> img;
temp = (real, img);
Arr[x][y] = temp;
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include "Header.h"
#include <complex>
#include <cmath>
usingnamespace std;
int main()
{
std::cout << "This program will calculate the exp of a matrix A\n";
ComplexMatrix z1(3,3);
z1.DisplayMatrix();
z1.EnterComplexMatrix(z1, 3, 3);
z1.DeleteMatrix();
}