Class arrays and pointers

Hi guys, I'm a C++ noob and have been out of the programming scene since highschool (was VB back then). I'm doing a unit on C++, and I'm trying my best to get a grasp of the use of pointers, references, etc.

Just FYI, I am using MS Visual C++ 2010 Express to do my coding.\

Right now I'm stuck on an activity to create a program that implements a complex number. The idea is that we are given the code for the class Complex, and the code to begin a function, and are left to fill in the gaps, so to speak. We are not allowed to change the original code given.

Here is the code given:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

const int MAX_ARRAY_SIZE = 5;

class Complex
{
public:
Complex(double a,double b):real(a),imaginary(b){};
double real;
double imaginary;
};


We have to write the code for the following function, which is intended to "search the index of a complex number pointed by k in a pointer to complex number array a using a function pointer equal":
int search(Complex *k, Complex *a[],bool (*equal)(Complex *,Complex *))

and in turn code the function equal that is called within this 'search' function.

Now I'm not asking for the code for the 'equal' and 'search' functions, at the moment I really just need to declare the right variables in the right way to begin with.

So for now I've just implemented these two functions as shells, to simply work on calling the search function without getting errors.

Shells for functions search and equal
1
2
3
4
5
6
7
8
9
10
11
12
13
// begin function search
int search(Complex *k, Complex *a[],bool (*equal)(Complex *,Complex *))
{
	int counter = 0;
	return counter;
} //end int search


// begin function equal
bool equal(Complex *comp1, Complex *comp2)
{
	return true;
}


Now I believe need to declare two variables: A Complex class (complex1) and a Complex class array (complex2[MAX_ARRAY_SIZE]).

I have tried a couple of ways to declare these, but comes up with an error every time I try to call the 'search' function.

Method 1:
1
2
3
4
5
6
7
8
9
// begin function main
int main()
{
	Complex complex1(5,6); // will get user to input values when complete
	Complex complex2[MAX_ARRAY_SIZE];

        // I just use the following code to test the function call
        cout << search(&complex1, &complex2, equal) << endl;
}


Method 2:
1
2
3
4
5
6
7
int main()
{
        Complex * complex1 = new Complex(5, 5);
	Complex * complex2 = new Complex[MAX_ARRAY_SIZE];

        cout << search(complex1, complex2, equal) << endl;
}


Now the first problem is I can only declare the Complex array in both methods when I change the default constructor to:
Complex( double a = 0, double b = 0 ): real( a ), imaginary( b ){};
Not sure if there's a way around this, but even though I don't think I'm allowed to in the activity, I'm starting to be beyond the point of caring for this technicality :P

My biggest concern is that I'm declaring the variables properly and entering the correct parameters when calling the search function.

In its current state, I get the error when calling the search function (when trying to use complex2 or &complex2 as parameter):
Error: argument of type "Complex (*)[5] is incompatible with parameter of type "Complex**"

I'm just at a loss. Like I said, all this is very new to me. I've been trying to use my textbook, google searches, etc as much as I can, but it seems passing a pointer of an array of a class is just perplexing right now.

Any suggestions/help/insight would be greatly appreciated and thank you in advance, I hope I have gotten the message across okay :P

closed account (D80DSL3A)
Hi.
Let's start with the problem of declaring an array when a no-arg constructor isn't available.
You can do it by initializing the array using a list of calls to the available constructor.
The following declares and initializes an array of 5 Complex objects:
 
Complex A[] = { Complex(1.0, 1.0), Complex(1.0, 2.0), Complex(1.0, 3.0), Complex(2.0, 1.0), Complex(3.0, 1.0) };


Next, are you stuck with this function declaration?
int search(Complex *k, Complex *a[],bool (*equal)(Complex *,Complex *))
That 2nd argument Complex *a[] is for an array of pointers, not a single pointer to an array.
Assuming it's OK to change this function to:
int search(Complex *k, Complex *a,bool (*equal)(Complex *,Complex *))
I got the following to work. The functions are still just shells but are enough to show that the scheme works:
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
bool Equal(Complex* a, Complex* b)
{
	return true;
}

int search(Complex *k, Complex *a, bool (*equal)(Complex *,Complex *))
{
	equal(k, (a+2));
	return 2;
}

int main(void)
{
	Complex A[] = { Complex(1.0, 1.0), Complex(1.0, 2.0), Complex(1.0, 3.0), Complex(2.0, 1.0), Complex(3.0, 1.0) };	

	Complex* pArray = A;// a pointer to the array
	Complex k(1.0, 3.0);// a complex # to search for

	int idx = search( &k, pArray, Equal );// passing pointers to k, the array and the Equal function

	cout << endl << "Index = " << idx;

	cout << endl;
	return 0;
}
Last edited on
Hey fun2code, thanks for your reply! It was a great help to get me started :)

I declared and intialied the array as you said, ie. Complex A[] = {...}. Worked fine.

I tried out the code you mentioned for declaring the function search, which also worked, and was great to help me get my head around what I was supposed to do. However, it was part of the question to use the function declaration as it was given. I had a play around and finally found a way to get it all to work.

Now, I'm not sure if what I have done is considered "bad coding", as I'm still getting my head around pointers, pointers to pointers, pointers to functions/arrays/etc. But the way I did it was to declare new Complex objects in the functions search and equal so that I could easily compare the values of the imaginary and real values. I came be this purely through trial and error.

Here is a copy of the whole program for you to have a gander:
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
#include <iostream>
using namespace std;

const int MAX_ARRAY_SIZE = 5;

class Complex
{
public:
// to initialise a default constructor to values of 0, use
//		Complex( double a = 0, double b = 0): real( a ), imaginary( b ){};
Complex( double a , double b ): real( a ), imaginary( b ){};
double real;
double imaginary;
};

int search( Complex *, Complex *[], bool (*)(Complex *, Complex*) ); //prototype for search
bool equal( Complex *, Complex *); // prototype for equal

// begin function main
int main()
{
	// Declare array of complex numbers
	// for no default constructor: Complex A[] = { Complex (double,double), ... }
	Complex A[MAX_ARRAY_SIZE] = { Complex( 1.0, 3.0 ), Complex( 1.0, 2.0 ), Complex( 3.0, 1.0 ),
									Complex( 1.0, 3.0 ), Complex( 2.0, 7.0 )};
	
	Complex * arrayPtr1 = A; // declare arrayPtr to be a pointer of A
	
	//show values of all complex numbers in A, for purpose of assignment
	cout << "ARRAY OF COMPLEX NUMBERS 'A' (for purpose of testing): " << endl;
	for ( int i = 0; i < MAX_ARRAY_SIZE; i++ )
		cout << "\nA[ " << i << " ] = \t( " << A[i].real << " + " << A[i].imaginary << "i )";
	
	
	double searchA, searchB; // values to be inputted by user
	
	// User inputs values for a and b, stored in searchA, searchB respectively
	cout << "\n\nComplex numbers are represented as ( a + bi )\n" << endl;
	cout << "Please enter a value for a: "; cin >> searchA;
	cout << "Please enter a value for b(i): "; cin >> searchB;

	Complex k(searchA, searchB); // declare complex number to be searched (k)
	
	// call the search function and store the result as integer idx
	int idx = search( &k, &arrayPtr1, equal);
	cout << "\n\nComplex number ( " << k.real << " + " << k.imaginary
		<< "i ) has been found " << idx << " times." << endl;

	return 0;
} // end main

// begin function search
int search(Complex *k, Complex *a[],bool (*equal)(Complex *,Complex *))
{
	int counter = 0; // counts the amount of times a true search function is returned
	
	// create a complex pointer to pass each member of array to equal function
	Complex * aPtr = a[0];
	
	// compare each value in the complex array a to the complex number k
	for (int i = 0; i < MAX_ARRAY_SIZE; i++)
	{
		// determine if function equal has returned 'true'
		if ((*equal)(k, &aPtr[i]))
			counter++;
	} // end for

	return counter;
} //end int search


// begin function equal
bool equal(Complex *a, Complex *b)
{
	// Declare complex numbers workA and workB to evaluate real and imaginary values
	Complex workA = *a, workB = *b;

	// return true if complex numbers workA and workB are the same
	return (workA.real == workB.real) && (workA.imaginary == workB.imaginary);
} // end function equal 


As you can see, I declared the Complex pointer aPtr in function search, and the Complex numbers workA and workB, equal to *a and *b respectively. It gained me easy access to the .real and .imaginary values, as I mentioned earlier.

I have no idea if there is an easier way to do this (probably is), but at least it's working. If you have any other methods to do this, I will be more than interested to find out.

Thanks again!
Topic archived. No new replies allowed.