Calling class member from function!

I have an assignment where I have to construct a nonmember function which calls a member function inside it.

The issue lies inside the testSuccesfulSearch function, where I have to call the object "obj" to call the member linearsearch (which is a member function inside class)

the error states: the object has type qualifers that are not compatible with the member function


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
#include <iostream>
#include <ctime>

using namespace std;

class Myclass
{
private:
	int *ptr; //pointer to 1D dynamic array 
	int size;
public:
	Myclass(int arraySize) //overload constructor 
	{
		size = arraySize;
		ptr = new int[size];
		cout << "Enter data for an array size: " << size << endl;
		for (int i = 0; i < size; i++)
		{
			cin >> ptr[i];
		}
		cout << endl;
	}
	int linearSearch(int searchItem); //linear search 
	~Myclass()
	{
		delete[] ptr;
		cout << "Memory reallocated!" << endl;
	}
};
int Myclass::linearSearch(int searchItem)
{
	srand(time(0));

	int item = searchItem;
	for (int i = 0; i < size; i++)
	{
		if (ptr[i] == item)
		{
			return i;
			break;
		}
	}
	return -1;
}

double testSuccesfulSearch(const Myclass &obj, long int runCount);

int main()
{
	Myclass B(10);
	float average;
	average = testSuccesfulSearch(B, 10000);
	system("PAUSE");
	return 0;
}
double testSuccesfulSearch(const Myclass &obj, long int runCount)
{
	obj.linearSearch(runCount);

}
The compiler is enforcing const-ness of your object. Notice the addition of const on lines 23 and 30.
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
#include <iostream>
#include <ctime>

using namespace std;

class Myclass
{
private:
    int *ptr; //pointer to 1D dynamic array 
    int size;
public:
    Myclass(int arraySize) //overload constructor 
    {
        size = arraySize;
        ptr = new int[size];
        cout << "Enter data for an array size: " << size << endl;
        for (int i = 0; i < size; i++)
        {
            cin >> ptr[i];
        }
        cout << endl;
    }
    int linearSearch(int searchItem) const; //linear search 
    ~Myclass()
    {
        delete[] ptr;
        cout << "Memory reallocated!" << endl;
    }
};
int Myclass::linearSearch(int searchItem) const
{
    srand(time(0));

    int item = searchItem;
    for (int i = 0; i < size; i++)
    {
        if (ptr[i] == item)
        {
            return i;
            break;
        }
    }
    return -1;
}

double testSuccesfulSearch(const Myclass &obj, long int runCount);

int main()
{
    Myclass B(10);
    float average;
    average = testSuccesfulSearch(B, 10000);
    system("PAUSE");
    return 0;
}
double testSuccesfulSearch(const Myclass &obj, long int runCount)
{
    obj.linearSearch(runCount);

}

In the function prototype for testSuccessfulSearch, you've stated to the compiler that obj is const. Therefore, the only functions that may be invoked on obj are those that are marked const.
Last edited on
So then do I have to make the object Myclass B constant in order for it to work?
No, just make the linearSearch function const by adding const in those two places I showed in my last post. What that does is tell the compiler "This method promises not to change the object that invokes it."
Ah interesting, I've never seen the const used like that before. Thank you, the program works fine now, however there is one slight issue left:

My program is intended to take the runcount, initialize it in the linearsearch function, and go through a loop of runcount times, creating a random number each time and searching for that random number in the array.

So for example: if my array consists of 10 integers and my run count is 10000, the loop will initialize 10000 times, creating a random number each time and searching for that said number in the array of 10 integers. If the number is found, I have to increment a counter, and return the counter to find the average number of times that number appeared

However, my program keeps outputting 0

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
#include <iostream>
#include <ctime>

using namespace std;

class Myclass
{
private:
	int *ptr; //pointer to 1D dynamic array 
	int size;
public:
	Myclass(int arraySize) //overload constructor 
	{
		size = arraySize;
		ptr = new int[size];
		cout << "Enter data for an array size: " << size << endl;
		for (int i = 0; i < size; i++)
		{
			cin >> ptr[i];
		}
		cout << endl;
	}
	int linearSearch(int searchItem) const; //linear search 
	void print();
	~Myclass()
	{
		delete[] ptr;
		cout << "Memory reallocated!" << endl;
	}
};
int Myclass::linearSearch(int searchItem) const
{
	srand(time(0));
	int random;
	int counter = 0;
	for (int i = 1; i <= searchItem; i++)
	{
		for (int j = 0; j < size; j++)
		{
			random = rand() & 10;
			if (ptr[j] == random)
			{
				counter++;
			}
		}
	}
	return counter;
}
void Myclass::print()
{
	for (int i = 0; i < size; i++)
	{
		cout << ptr[i] << " ";
	}
	cout << endl;
}

double testSuccesfulSearch(const Myclass &obj, long int runCount);

int main()
{
	Myclass B(10);
	float average;
	int count;
	count = 10000;
	average = testSuccesfulSearch(B, count);

	cout << "Array size: 10" << endl;
	cout << "Array content: ";
	B.print();

	cout << "Run count: " << count << endl;
	cout << "Average number of comparisons over " << count << " random searches: " << average << endl;



	system("PAUSE");
	return 0;
}
double testSuccesfulSearch(const Myclass &obj, long int runCount)
{
	int comp;
	comp = obj.linearSearch(runCount);
	return float(comp / runCount); //cast 
}
Topic archived. No new replies allowed.