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>
usingnamespace 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?
#include "Header.h"
#include "candidates.h"
#include <iostream>
#include <string>
usingnamespace 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
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.
#include "Header.h"
#include "candidates.h"
#include <iostream>
#include <string>
usingnamespace 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;
}
}
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.