How can I assign variables to a derived class from the base class within a constructor?

Hello,

I have declared 2 classes, 1 for a matrix and another for complex numbers. I would like to use a constructor to dynamically allocate the array and then fill it with complex number objects. I am stuck on how to actually make the constructor so I can assign default values to the array

Errors I have received
in Header1.h
Line 19 = Expected a ;
Line 21 = expected an expression
Line 27 = Expected and identifier

Header1.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
#pragma once

#include "Header.h"

using namespace std;

class Matrix : public ComplexNumber
{
public:
    int i;
    int j;
    ComplexNumber** Matrix = new ComplexNumber* [i];
    void InitialiseMatrix(ComplexNumber**, int, int);
    void DeleteMatrix(ComplexNumber**, int, int);
    void EnterMatrixElements(ComplexNumber**, int, int);
    void DisplayMatrix(ComplexNumber**, int, int);
};

Matrix::ComplexNumber()
{
    for (int x = 0; x < rows; x++)
    {
        Matrix[x] = new double[cols];
    }
}

void Matrix::InitialiseMatrix(ComplexNumber** A, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            A[i][j] = ComplexNumber(0.0, 0.0);
        }
    }
}

void Matrix::DeleteMatrix(ComplexNumber** A, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        delete[]A[i];
    }
    delete[] A;
}

void Matrix::EnterMatrixElements(ComplexNumber** A, int rows, int cols)
{
    cout << "What data would you like to enter for the matrix?" << endl;
    cout << "The matrix has a size of " << rows * cols << " elements" << endl;

    double real;
    double img;

    for (int x = 0; x < rows; x++)
    {
        for (int y = 0; y < cols; y++)
        {
            cout << "What is the real number you would like to enter?" << endl;
            cin >> real;
            cout << "What is the imaginary number you would like to enter?" << endl;
            cin >> img;
            A[x][y] = ComplexNumber(real, img);
        }
    }
}

void DisplayMatrix(ComplexNumber**, int, int)
{
    cout << "{";
    for (int i = 0; i < rows; i++)
    {
        cout << "{";
        for (int j = 0; j < columns; j++)
        {
            cout << setprecision(3) << setw(6) << matrix[i][j] << ", ";
        }
        cout << "}" << endl;
    }
    cout << "}";
}


Main
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
#include <iostream>
#include "Header.h"
#include "Header1.h"

using namespace std;

int main()
{
	//Matrix size 
	int i = 3;
	int j = 3;

	ComplexNumber z1(4.0, 3.0);
	std::cout << "z1 = " << z1 << "\n"; 
	std::cout << "Modulus z1 = "	<< z1.CalculateModulus() << "\n";
	std::cout << "Argument z1 = "	<< z1.CalculateArgument() << "\n";

	ComplexNumber z2; 
	z2 = z1.CalculatePower(3);
	std::cout << "z2 = z1*z1*z1 = " << z2 << "\n";

	ComplexNumber z3; z3 = -z2;
	std::cout << "z3 = -z2 = " << z3 << "\n";
	
	ComplexNumber z4; 
	z4 = z1 + z2;
	std::cout << "z1 + z2 = " << z4 << "\n";
	
	ComplexNumber zs[2]; 
	zs[0] = z1; 
	zs[1] = z2;
	std::cout << "Second element of zs = " << zs[1] << "\n";

	A = Matrix(i, j);
	A.DisplayMatrix(A, i, j);

	return 0;
}


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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
#ifndef COMPLEXNUMBERHEADERDEF
#define COMPLEXNUMBERHEADERDEF

#include <iostream>
#include <iomanip>

using namespace std;

class ComplexNumber
{
private:
    double mRealPart;
    double mImaginaryPart;

public:
    //Constructors for complex numbers
    ComplexNumber();
    ComplexNumber(double x, double y);

    //Member Functions for class
    double CalculateModulus() const;
    double CalculateArgument() const;
    ComplexNumber CalculatePower(double n) const;

    //Overloaded operators for complex numbers
    ComplexNumber& operator=(const ComplexNumber& z);
    ComplexNumber& operator=(double& z);
    ComplexNumber operator-() const;
    ComplexNumber operator+(const ComplexNumber& z) const;
    ComplexNumber operator-(const ComplexNumber& z) const;
    friend std::ostream& operator<<(std::ostream& output,
        const ComplexNumber& z);
};

// Override default constructor
// Set real and imaginary parts to zero
ComplexNumber::ComplexNumber()
{
    mRealPart = 0.0;
    mImaginaryPart = 0.0;
}

// Constructor that sets complex number z=x+iy
ComplexNumber::ComplexNumber(double x, double y)
{
    mRealPart = x;
    mImaginaryPart = y;
}


// Method for computing the modulus of a
// complex number
double ComplexNumber::CalculateModulus() const
{
    return sqrt(mRealPart * mRealPart +
        mImaginaryPart * mImaginaryPart);
}

// Method for computing the argument of a
// complex number
double ComplexNumber::CalculateArgument() const
{
    return atan2(mImaginaryPart, mRealPart);
}

// Method for raising complex number to the power n
// using De Moivre's theorem - first complex
// number must be converted to polar form
ComplexNumber ComplexNumber::CalculatePower(double n) const
{
    double modulus = CalculateModulus();
    double argument = CalculateArgument();
    double mod_of_result = pow(modulus, n);
    double arg_of_result = argument * n;
    double real_part = mod_of_result * cos(arg_of_result);
    double imag_part = mod_of_result * sin(arg_of_result);
    ComplexNumber z(real_part, imag_part);
    return z;
}

// Overloading the = (assignment) operator
ComplexNumber& ComplexNumber::operator=(const ComplexNumber& z)
{
    mRealPart = z.mRealPart;
    mImaginaryPart = z.mImaginaryPart;
    return *this;
}

//Overloading the assignment operator so it can assign a complex number from a double object
ComplexNumber& ComplexNumber::operator=(double& z)
{
    ComplexNumber w(z,0.0);
    return *this;
}

// Overloading the unary - operator
ComplexNumber ComplexNumber::operator-() const
{
    ComplexNumber w;
    w.mRealPart = -mRealPart;
    w.mImaginaryPart = -mImaginaryPart;
    return w;
}

// Overloading the binary + operator
ComplexNumber ComplexNumber::
operator+(const ComplexNumber& z) const
{
    ComplexNumber w;
    w.mRealPart = mRealPart + z.mRealPart;
    w.mImaginaryPart = mImaginaryPart + z.mImaginaryPart;
    return w;
}

// Overloading the binary - operator
ComplexNumber ComplexNumber::
operator-(const ComplexNumber& z) const
{
    ComplexNumber w;
    w.mRealPart = mRealPart - z.mRealPart;
    w.mImaginaryPart = mImaginaryPart - z.mImaginaryPart;
    return w;
}

// Overloading the insertion << operator
std::ostream& operator<<(std::ostream& output,
    const ComplexNumber& z)
{
    // Format as "(a + bi)" or as "(a - bi)"
    output << "(" << z.mRealPart << " ";
    if (z.mImaginaryPart >= 0.0)
    {
        output << "+ " << z.mImaginaryPart << "i)";
    }
    else
    {
        // z.mImaginaryPart < 0.0
        // Replace + with minus sign 
        output << "- " << -z.mImaginaryPart << "i)";
    }
    return output;
}
#endif
Last edited on
Putting aside that your inheritance model doesn't make any sense (in your code, a matrix is a kind of complex number, which just makes no sense - surely your matrix should CONTAIN complex numbers, not be a complex number?), this:

Matrix::ComplexNumber()

Is this meant to be the constructor of Matrix? It should be:
Matrix::Matrix()

If you want to set values in the base class, then at this point you call the constructor of that base class, like this:

Matrix::Matrix(double x, double y) : ComplexNumber( x , y)

This really shows that the inheritance makes no sense. A Matrix should contain lots of ComplexNumber obejcts, it should not itself be one ComplextNumber.
Last edited on
Hello

Yes, this seems silly in the design of it. I will try a new implementation by including it in the ComplexNumber class to begin with.
by including it in the ComplexNumber class to begin with.


Still doesn't make much sense. Why would a complex number contain a matrix of complex numbers? And each one of those complex numbers, in the matrix, would then contain a matrix of complex numbers? And so on to infinity?

I think you're looking for something like this:
1
2
3
4
5
6
7
8
9
class Matrix
{
  ComplexNumber** array_of_complex_numbers;

  Matrix(int number_of_row, int number_of_columns)
  {
    // in here, make "array_of_complex_numbers" an actual 2D array of complexNumber objects
  }
};


Matrix should simply be a class that manages and contains a 2D array of ComplexNumber objects.

It doesn't make sense at all. I was trying to mess around with class to class conversions, inheritance etc. I did just compound and grow more errors when I was writing it! I did get the the feeling that there was something wrong in the way I modelled it in my head.

This is far simpler as far as way to store an array of pointer objects to complex numbers. This is a far simpler class design that I will implement instead. Unfortunately I cannot compile it due to the following errors

Severity Code Description Project Line
Error LNK1120 1 unresolved externals 6.7_Exercises_7 1

Error LNK2019 unresolved external symbol "public: double & __cdecl
ComplexNumber::operator=(class ComplexNumber &)" (??
4ComplexNumber@@QEAAAEANAEAV0@@Z) referenced in function
main 1


New Header
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <iomanip>

using namespace std;

class ComplexNumber
{
private:
    double mRealPart;
    double mImaginaryPart;

public:
    //Constructors for complex numbers
    ComplexNumber();
    ComplexNumber(double x, double y);
    
    //Member Functions for class
    double CalculateModulus() const;
    double CalculateArgument() const;
    ComplexNumber CalculatePower(double n) const;

    //Overloaded operators for complex numbers
    ComplexNumber& operator=(const ComplexNumber& z);
    ComplexNumber& operator=(double& z);
    double& operator=(ComplexNumber& z);
    ComplexNumber operator-() const;
    ComplexNumber operator+(const ComplexNumber& z) const;
    ComplexNumber operator-(const ComplexNumber& z) const;
    friend std::ostream& operator<<(std::ostream& output,
        const ComplexNumber& z);
};

// Override default constructor
// Set real and imaginary parts to zero
ComplexNumber::ComplexNumber()
{
    mRealPart = 0.0;
    mImaginaryPart = 0.0;
}
// Constructor that sets complex number z=x+iy
ComplexNumber::ComplexNumber(double x, double y)
{
    mRealPart = x;
    mImaginaryPart = y;
}

// Method for computing the modulus of a
// complex number
double ComplexNumber::CalculateModulus() const
{
    return sqrt(mRealPart * mRealPart +
        mImaginaryPart * mImaginaryPart);
}

// Method for computing the argument of a
// complex number
double ComplexNumber::CalculateArgument() const
{
    return atan2(mImaginaryPart, mRealPart);
}

// Method for raising complex number to the power n
// using De Moivre's theorem - first complex
// number must be converted to polar form
ComplexNumber ComplexNumber::CalculatePower(double n) const
{
    double modulus = CalculateModulus();
    double argument = CalculateArgument();
    double mod_of_result = pow(modulus, n);
    double arg_of_result = argument * n;
    double real_part = mod_of_result * cos(arg_of_result);
    double imag_part = mod_of_result * sin(arg_of_result);
    ComplexNumber z(real_part, imag_part);
    return z;
}

// Overloading the = (assignment) operator
ComplexNumber& ComplexNumber::operator=(const ComplexNumber& z)
{
    mRealPart = z.mRealPart;
    mImaginaryPart = z.mImaginaryPart;
    return *this;
}

//Overloading the assignment operator so it can assign a complex number from a double object
ComplexNumber& ComplexNumber::operator=(double& z)
{
    ComplexNumber w(z,0.0);
    return *this;
}

// Overloading the unary - operator
ComplexNumber ComplexNumber::operator-() const
{
    ComplexNumber w;
    w.mRealPart = -mRealPart;
    w.mImaginaryPart = -mImaginaryPart;
    return w;
}

// Overloading the binary + operator
ComplexNumber ComplexNumber::operator+(const ComplexNumber& z) const
{
    ComplexNumber w;
    w.mRealPart = mRealPart + z.mRealPart;
    w.mImaginaryPart = mImaginaryPart + z.mImaginaryPart;
    return w;
}

// Overloading the binary - operator
ComplexNumber ComplexNumber::operator-(const ComplexNumber& z) const
{
    ComplexNumber w;
    w.mRealPart = mRealPart - z.mRealPart;
    w.mImaginaryPart = mImaginaryPart - z.mImaginaryPart;
    return w;
}

// Overloading the insertion << operator
std::ostream& operator<<(std::ostream& output, const ComplexNumber& z)
{
    // Format as "(a + bi)" or as "(a - bi)"
    output << "(" << z.mRealPart << " ";
    if (z.mImaginaryPart >= 0.0)
    {
        output << "+ " << z.mImaginaryPart << "i)";
    }
    else
    {
        // z.mImaginaryPart < 0.0
        // Replace + with minus sign 
        output << "- " << -z.mImaginaryPart << "i)";
    }
    return output;
}


class Matrix
{
private:
    ComplexNumber** Arr;
    int i = 0; 
    int j = 0;

public:
    Matrix(int i, int j);
    void DisplayMatrix(ComplexNumber**, int, int);
};


class Matrix
{
private:
    ComplexNumber** Arr;
    int i = 0; 
    int j = 0;

public:
    Matrix(int i, int j);
    void DisplayMatrix(ComplexNumber**, int, int);
};

Matrix::Matrix(int i, int j)
{
    Arr = new ComplexNumber * [i];

    for (int x = 0; x < i; x++)
    {
        Arr[x] = new ComplexNumber[j];
    }

    for (int x1 = 0; x1 < i; x1++)
    {
        for (int y1 = 0; y1 < j; y1++)
        {
            Arr[x1][y1] = ComplexNumber(0.0, 0.0);
        }
    }
}

void Matrix::DisplayMatrix(ComplexNumber** Arr, int i, int j)
{
    for (int x1 = 0; x1 < i; x1++)
    {
        for (int y1 = 0; y1 < j; y1++)
        {
            cout << Arr[x1][y1];
        }
    }
}

#endif 
Last edited on
How can I assign variables to a derived class from the base class within a constructor?


I didn't look at code but usually you inherit base constructor if your derived doesn't need parameters for constructor.

ex. put into your derived class:
using Base::Base;

Otherwise if that's not possible, use delegating constructor in your derived class
ex. define your derived ctor:

1
2
3
4
5
Matrix::Matrix(int i, int j) :
        ComplexNumber(4.1, 4.2) // feed base construtor with values.
{
      // ...
}


This are just examples, you may need to modify your ctor's, so that you can feed base ctor from derived ctor.
Last edited on
Hello,

So from further editing, I have the following issue perisiting with lines 30-31 in my main.

no suitable conversion function from "Matrix" to "ComplexNumber **" exists
'void Matrix::DisplayMatrix(ComplexNumber **,int,int)': cannot convert argument 1 from 'Matrix' to 'ComplexNumber **'

This are just examples, you may need to modify your ctor's, so that you can feed base ctor from derived ctor


I think I did this in the header code. Within this constructor below, I did fill it with Complex Numbers, do I have to pass a pointer to the complex number then a pointer to the matrix?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Matrix::Matrix(int mi, int mj)
{
    mArr = new ComplexNumber* [mi];

    for (int x = 0; x < mi; x++)
    {
        mArr[x] = new ComplexNumber[mj];
    }

    // Initialise array with 0s real and imaginary 
    for (int x1 = 0; x1 < mi; x1++)
    {
        for (int y1 = 0; y1 < mj; y1++)
        {
            mArr[x1][y1] = ComplexNumber(0.0, 0.0);
        }
    }
}


Header
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#pragma once
#ifndef COMPLEXNUMBERHEADERDEF
#define COMPLEXNUMBERHEADERDEF

#include <iostream>

using namespace std;

class ComplexNumber
{
private:
    double mRealPart;
    double mImaginaryPart;
public:
    ComplexNumber();
    ComplexNumber(double x, double y);
    double CalculateModulus() const;
    double CalculateArgument() const;
    ComplexNumber CalculatePower(double n) const;
    ComplexNumber& operator=(const ComplexNumber& z);
    ComplexNumber operator-() const;
    ComplexNumber operator+(const ComplexNumber& z) const;
    ComplexNumber operator-(const ComplexNumber& z) const;
    friend std::ostream& operator<<(std::ostream& output,
        const ComplexNumber& z);
};


// Override default constructor
// Set real and imaginary parts to zero
ComplexNumber::ComplexNumber()
{
    mRealPart = 0.0;
    mImaginaryPart = 0.0;
}

// Constructor that sets complex number z=x+iy
ComplexNumber::ComplexNumber(double x, double y)
{
    mRealPart = x;
    mImaginaryPart = y;
}

// Method for computing the modulus of a
// complex number
double ComplexNumber::CalculateModulus() const
{
    return sqrt(mRealPart * mRealPart +
        mImaginaryPart * mImaginaryPart);
}

// Method for computing the argument of a
// complex number
double ComplexNumber::CalculateArgument() const
{
    return atan2(mImaginaryPart, mRealPart);
}

// Method for raising complex number to the power n
// using De Moivre's theorem - first complex
// number must be converted to polar form
ComplexNumber ComplexNumber::CalculatePower(double n) const
{
    double modulus = CalculateModulus();
    double argument = CalculateArgument();
    double mod_of_result = pow(modulus, n);
    double arg_of_result = argument * n;
    double real_part = mod_of_result * cos(arg_of_result);
    double imag_part = mod_of_result * sin(arg_of_result);
    ComplexNumber z(real_part, imag_part);
    return z;
}

// Overloading the = (assignment) operator
ComplexNumber& ComplexNumber::operator=(const ComplexNumber& z)
{
    mRealPart = z.mRealPart;
    mImaginaryPart = z.mImaginaryPart;
    return *this;
}

// Overloading the unary - operator
ComplexNumber ComplexNumber::operator-() const
{
    ComplexNumber w;
    w.mRealPart = -mRealPart;
    w.mImaginaryPart = -mImaginaryPart;
    return w;
}

// Overloading the binary + operator
ComplexNumber ComplexNumber::operator+(const ComplexNumber& z) const
{
    ComplexNumber w;
    w.mRealPart = mRealPart + z.mRealPart;
    w.mImaginaryPart = mImaginaryPart + z.mImaginaryPart;
    return w;
}

// Overloading the binary - operator
ComplexNumber ComplexNumber::operator-(const ComplexNumber& z) const
{
    ComplexNumber w;
    w.mRealPart = mRealPart - z.mRealPart;
    w.mImaginaryPart = mImaginaryPart - z.mImaginaryPart;
    return w;
}

// Overloading the insertion << operator
std::ostream& operator<<(std::ostream& output,const ComplexNumber& z)
{
    // Format as "(a + bi)" or as "(a - bi)"
    output << "(" << z.mRealPart << " ";
    if (z.mImaginaryPart >= 0.0)
    {
        output << "+ " << z.mImaginaryPart << "i)";
    }
    else
    {
        // z.mImaginaryPart < 0.0
        // Replace + with minus sign 
        output << "- " << -z.mImaginaryPart << "i)";
    }
    return output;
}

class Matrix
{
private:
    ComplexNumber** mArr;
    int mi = 3;
    int mj = 3;
    
public:
    Matrix(int i, int j);
    void DeleteMatrix(ComplexNumber**, int, int);
    void DisplayMatrix(ComplexNumber**, int, int);
};

//Constructor for complex number matrix, initialising it to 0 for real and imaginary numbers
Matrix::Matrix(int mi, int mj)
{
    mArr = new ComplexNumber* [mi];

    for (int x = 0; x < mi; x++)
    {
        mArr[x] = new ComplexNumber[mj];
    }

    // Initialise array with 0s real and imaginary 
    for (int x1 = 0; x1 < mi; x1++)
    {
        for (int y1 = 0; y1 < mj; y1++)
        {
            mArr[x1][y1] = ComplexNumber(0.0, 0.0);
        }
    }
}

//Delete matrix
void Matrix::DeleteMatrix(ComplexNumber** Arr, int i, int j)
{
    for (int x = 0; x < i; x++)
    {
        delete[] Arr[x];
    }
    delete[] Arr;
}

//Member function to display matrix
void Matrix::DisplayMatrix(ComplexNumber** A, int i, int j)
{
    for (int x1 = 0; x1 < i; x1++)
    {
        for (int y1 = 0; y1 < j; y1++)
        {
            cout << A[x1][y1];
        }
    }
}

#endif 


Main
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
#include <iostream>
#include "Header.h"

using namespace std;

int main()
{
	ComplexNumber z1(4.0, 3.0);

	std::cout << "z1 = " << z1 << "\n"; std::cout << "Modulus z1 = " << z1.CalculateModulus() << "\n";
	std::cout << "Argument z1 = " << z1.CalculateArgument() << "\n";

	ComplexNumber z2;
	z2 = z1.CalculatePower(3);
	std::cout << "z2 = z1*z1*z1 = " << z2 << "\n";

	ComplexNumber z3;
	z3 = -z2;
	std::cout << "z3 = -z2 = " << z3 << "\n";

	ComplexNumber z4;
	z4 = z1 + z2;
	std::cout << "z1 + z2 = " << z4 << "\n";
	ComplexNumber zs[2];
	zs[0] = z1;
	zs[1] = z2;

	std::cout << "Second element of zs = " << zs[1] << "\n";

	Matrix A(3, 3);
	A.DisplayMatrix(A, 3, 3);

	return 0;
}
Last edited on
do I have to pass a pointer to the complex number then a pointer to the matrix?

You need to pass the types of arguments that you've told the compiler you're going to pass. To wit:

void DisplayMatrix(ComplexNumber**, int, int);


(You could, if you really wanted to, define a conversion operator for the Matrix class, so that you could pass it into a function expecting a ComplexNumber**, if you were really wedded to the idea of being able to pass a Matrix.

EDIT: Although calling a method of an object, and having to pass in that same object as an argument, would be weird anyway. Would make more sense to simply have the method operate directly on the object's own data members, no?)
Last edited on
Hi,

I think I have given up on this. I think using the existing <complex> library would be far easier to work with. I will try this I think.
1
2
3
#pragma once
#ifndef COMPLEXNUMBERHEADERDEF
#define COMPLEXNUMBERHEADERDEF 


You don't need both of these. One or the other will do.
Topic archived. No new replies allowed.