calling function from classes

Im trying to call a function from a class called report but i cannot call i have tried calling it like this Candidate::report(output,x),Candidate rep; rep.report; and none of the seem to be working any help would be greatly appreciated

Header file
#ifndef CANDIDATES_H
#define CANDIDATES_H

#include <iostream>
#include <string>

/**
* Information on the political candidates in the primaries.
*/

class Candidate {
std::string name;
int delegatesWon;

public:
Candidate (std::string candidateName);//Constructor w/parameters

std::string getName() {return name;}
void setName(std::string nam) {name = nam;}

int getNumberOfDelegatesWon() {return delegatesWon;}
void setNumberOfDelegatesWon(int nDelegates) {delegatesWon = nDelegates;}
void addDelegatesWon(int nDelegates) {delegatesWon += nDelegates;}

/**
* Print the report line for the indicated candidate
*/
void report (std::ostream& out, int totalDelegatesAllStates);
};

#endif

cpp file for header
#include "candidates.h"
#include "states.h"

#include <iostream>

using namespace std;


Candidate::Candidate (string candidateName)
{
name = candidateName;
delegatesWon = 0;

}

/**
* Print the report line for the indicated candidate
*/
void Candidate::report (ostream& out, int totalDelegates)
{cout<<"try";
int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
if (delegatesWon >= requiredToWin)
out << "* ";
else
out << " ";

out << delegatesWon << " " << name << endl;
}
main function




#include "candidates.h"
#include "candidateCollections.h"
#include "states.h"
#include <iostream>
#include <fstream>


using namespace std;


void primaryElection (istream& input, ostream& output);


int main(int argc, char** argv)
{
// Main routine - if command parameters give two filenames,
// read from the first and write to the second. If not,
// read from standard input and write to standard output
if (argc == 3)
{cout<<"yeah";
ifstream in (argv[1]);
ofstream out (argv[2]);
primaryElection (in, out);
}
else
primaryElection (cin, cout);

return 0;
}





void primaryElection (istream& input, ostream& output)
{

int x=0;

Candidate rep;
rep.report(output,x);

}
I don't think the problem is in rep.report(output,x); but rather in
1
2
else
    primaryElection(cin, cout);


You seem to be using cin and cout (keywords) to attempt to pass variables, when they are not able to.
In addition to what ciphermagi has pointed out,

Check the arguments in the calling function "primaryFunction" in this block:

1
2
3
4
5
6
7
8
if (argc == 3)
{cout<<"yeah";
ifstream in (argv[1]);
ofstream out (argv[2]);
primaryElection (in, out);
}
else
primaryElection (cin, cout);


Also, in the definition of "report function", you are passing "0" as a value for "totalDelegate". delegateWon already is initialized to "0" and nothing is changing its value so the condition
if (delegatesWon >= requiredToWin)

will always be false except when requiredToWin is "0"

1
2
3
4
5
6
7
8
9
10
void Candidate::report (ostream& out, int totalDelegates)
{cout<<"try";
int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
if (delegatesWon >= requiredToWin)
out << "* ";
else
out << " ";

out << delegatesWon << " " << name << endl;
}
http://cplusplus.com/forum/articles/40071/#msg218019

There is no problem in passing the cin, cout objects as parameters to functions.
Topic archived. No new replies allowed.