How do you pass a class as a paramater into a function

How do you pass a class as a paramater into a function?

 
string readNames(int, int, candidates[]);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Header.h"
#include "candidates.h"
#include <iostream>
#include <string>

using namespace std;
int main()
{
	int num = 0, j = 0;
	cout << "Enter the number of candidates: ";
	cin >> num;
	cin.ignore();
	candidates* c = new candidates[num];
	
	readNames(num, j, c);


I get an error when I call the readNames function. What is the proper way of doing this?
Last edited on
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
#include "Header.h"
#include "candidates.h"
#include <iostream>
#include <string>

using namespace std;
int main()
{
	int num = 0, j = 0;
	cout << "Enter the number of candidates: ";
	cin >> num;
	cin.ignore();
	candidates* c = new candidates[num];
	
	readNames(c, num, j);

	cout << "\nType <end> to close the program";
	string vote;
	do
	{
		cout << "\nEnter the name of the candidate you woulld like to vote for: ";
		getline(cin, vote);
		for (int j = 0; j < num; j++)
		{
			if (vote == c[j].getName())
			{
				c[j].incrementVotes();
			}
		}
		
	} 
	while (vote != "end");
	
	for (int k = 0; k < num; k++)
	{
		cout << "\nno. of votes for " << c[k].getName() << ": "<< c[k].incrementVotes() << endl;
	}
	
	delete[] c;

	system("PAUSE");
	return 0;
}

void readNames(int num, int j, candidates c[])
{
	string Cname;
	for (int i = 0; i < num; i++)
	{
		j++;
		cout << "\nEnter the name of Candidate no. " << j << ": ";
		getline(cin, Cname);
		//c[i]->setName(Cname);
		c[i].setName(Cname);
		cout << endl;
	}
	 
}


Full code.

The code worked before, I am just trying to put them into functions for easier understanding
Last edited on
You are attempting to use the readNames function before it is either declared or defined. The identifier readNames is not valid where you've used it on line 15.

Declare the function prior to main:

1
2
3
4
5
6
7
8
9
10
11
12
13
// ...

void readNames(int, int, candidates[]);  // declaration

int main()
{
    // ...
}

void readNames(int num, int j, candidates c[])
{
    // definition here.
}
Last edited on
It is declared in my header file
It is declared in my header file

Is it declared as a member of the candidates class or as a free standing function?

Can you relate the actual text of the error received instead of just saying "I get an error?"
Is it declared as a member of the candidates class or as a free standing function?

I am not sure what you mean, it is as follows in my header file

void readNames(candidates[], int, int);

Can you relate the actual text of the error received instead of just saying "I get an error?"

Sure.

Severity Code Description Project File Line
Error C2059 syntax error: ']' 17t j:\private work\javatoc++\week 9\17t\17t\header.h 2


Severity Code Description Project File Line
Error C3861 'readNames': identifier not found 17t j:\private work\javatoc++\week 9\17t\17t\source.cpp 73


Severity Code Description Project File Line
Error C2065 'candidates': undeclared identifier 17t j:\private work\javatoc++\week 9\17t\17t\header.h 2



Updated code:
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
#include "Header.h"
#include "candidates.h"
#include <iostream>
#include <string>

using namespace std;
int main()
{
	int num = 0, j = 0;
	cout << "Enter the number of candidates: ";
	cin >> num;
	cin.ignore();
	candidates* c = new candidates[num];
	
	readNames(c, num, j);

	cout << "\nType <end> to close the program";
	string vote;
	do
	{
		cout << "\nEnter the name of the candidate you woulld like to vote for: ";
		getline(cin, vote);
		for (int j = 0; j < num; j++)
		{
			if (vote == c[j].getName())
			{
				c[j].incrementVotes();
			}
		}
		
	} 
	while (vote != "end");
	
	for (int k = 0; k < num; k++)
	{
		cout << "\nno. of votes for " << c[k].getName() << ": "<< c[k].incrementVotes() << endl;
	}
	
	delete[] c;

	system("PAUSE");
	return 0;
}

void readNames(candidates c[], int num, int j)
{
	string Cname;
	for (int i = 0; i < num; i++)
	{
		j++;
		cout << "\nEnter the name of Candidate no. " << j << ": ";
		getline(cin, Cname);
		//c[i]->setName(Cname);
		c[i].setName(Cname);
		cout << endl;
	}
	 
}
Last edited on
Thanks for the help, tho I figured it out. I needed to include "Header.h" under "candidates.h" and not the other way round.

i.e.

1
2
#include "candidates.h"
#include "Header.h" 


Thanks for the help, it was my silly mistake.
cire wrote:
Is it declared as a member of the candidates class or as a free standing function?
imohamme5 wrote:
I am not sure what you mean, it is as follows in my header file
void readNames(candidates[], int, int);


I mean does the declaration you show above appear inside the class definition or outside the class definition? You're providing snippets of code without the context needed to fully understand them. Given the code in the .cpp file and the error message, I think it's safe to say that it occurs inside the class.

Member functions which are non-static require an object to operate on. For instance, it would make no sense to write line 54 as setName(Cname);.

This should be a free standing function or a static member of the class (which doesn't require an object of the type your defining to operate on.)

If you go with a free standing function, only the declaration of the function needs to change (be moved outside the class definition.) If you go with a static member function, you'll need to change the function declaration, the function definition and how you call the function.
Last edited on
Thanks for the help, tho I figured it out. I needed to include "Header.h" under "candidates.h" and not the other way round.

If inclusion order matters, you're doing something wrong.
Topic archived. No new replies allowed.