Linear Search using a class

So I am running into a problem in which I want to use a constructor to initialize my member variable array as soon as I define an object of the class. However, The second value in the initialization list "4520125" is in red that reads, "Too many initializer values." And of course, the it gives me, " '=' cannot convert initializer list to int."

I am still new to it.

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>
using namespace std;


const int SIZE = 18;

class AccountNumber
{
private:
	int valueKey;
	int accNums[18];

public:
	AccountNumber();

	void setValueKey(int k)
	{
		valueKey = k;
	}

	int linearSearch();

};


AccountNumber::AccountNumber()
{
	 accNums[18] =      { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
						  8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
						  1005231, 654231,  3852085, 7576651, 7881299, 4581002 };
}

int AccountNumber::linearSearch()
{
	int index = 0;
	int position = -1;
	bool found = false;

	while (index < SIZE && !found)
	{
		if (accNums[index] == valueKey)
		{
			found = true;
			position = index;
		}
		index++;
	}


	return position;
}

void displayResults(const int, const int);

int main()
{
	AccountNumber obj;



	int userChoice;
	int results;

	cout << "Enter a charge account number: ";
	cin >> userChoice;
	obj.setValueKey(userChoice);

	results = obj.linearSearch();

	displayResults(userChoice, results);
	return 0;
}


void displayResults(const int userChoice, const int results)
{

	if (results == -1)
		cout << "\nThe charge account number is invalid.\n";
	else
	{
		cout << "\nThe charge account number " << userChoice << " was found\n"
			 << "in subscript " << results << " of the array.\n";
	}
}
Either initialize it directly in the class body ...

1
2
3
4
5
6
7
8
class AccountNumber
{
private:
	int valueKey;
	int accNums[18] = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
	                    8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
	                    1005231, 654231,  3852085, 7576651, 7881299, 4581002 }
	...

... or use the member initializer list.

1
2
3
4
5
6
AccountNumber::AccountNumber()
:	accNums { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
	          8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
	          1005231, 654231,  3852085, 7576651, 7881299, 4581002 }
{
}


I see. I must've mistaken that classes were the same as structures and couldn't be initialized inside of it.

Also didn't know that it'll work by omitting the assignment operator in the constructor.

Thank! :D
Also didn't know that it'll work by omitting the assignment operator in the constructor.

If you look closely you'll see that I didn't just leave out the "assignment operator". Search for "member initializer list" if you want to know more.
Yes. It seems i added the data type int again when I should't have. Thanks and will do.
Topic archived. No new replies allowed.