Error in assigning complex numbers to a class object array of complex numbers

Hello,

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


Header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once
#include <iostream>
#include <complex>
#include <cmath>

using namespace std;

class ComplexMatrix
{
private:
	complex<long double>** 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<long double> * [mi];

	for (int x = 0; x < mi; x++)
	{
		Arr[x] = new complex<long double>  [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<long double> * [i];

	for (int x = 0; x < i; x++)
	{
		Arr[x] = new complex<long double>[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 < long double> 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>

using namespace 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();
} 
Remove the first parameter from EnterComplexMatrix(ComplexMatrix& Arr, int i, int j)
Hi,

You know I had the same problem with the previous member function calls, and chose not to do the same thing to the this one.


............................................________
....................................,.-'"...................``~.,
.............................,.-"..................................."-.,
.........................,/...............................................":,
.....................,?......................................................,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:"........./
..............?.....__.........................................:`.........../
............./__.(....."~-,_..............................,:`........../
.........../(_...."~,_........"~,_....................,:`........_/
..........{.._$;_......"=,_......."-,_.......,.-~-,},.~";/....}
...........((.....*~_......."=-._......";,,./`..../"............../
...,,,___.`~,......"~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-"
............/.`~,......`-...................................../
.............`~.*-,.....................................|,./.....,__
,,_..........}.>-._...................................|..............`=~-,
.....`=~-,__......`,.................................
...................`=~-,,.,...............................
................................`:,,...........................`..............__
.....................................`=-,...................,%`>--==``
........................................_..........._,-%.......`
...................................,
Topic archived. No new replies allowed.