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