Derek H Derek H - 1 year ago 71
C++ Question

Trying to pass ifstream through a function?

I'm having trouble passing

along with a
and an
. It won't let me just use
void readSettings(settings, key, numOfQuestions);
and then when I pass it as a reference, it says key and numOfQuestions are not type names. Any solution?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void readSettings(ifstream&, string&, int&);

int main() {

string key = "";
int numOfQuestions = 0;

void readSettings(ifstream& settings, key, numOfQuestions);

cout << key;
cout << numOfQuestions;

return 0;

void readSettings(ifstream& settings, string& key, int& numOfQuestions)
// To be completed"settings.txt");

settings >> numOfQuestions;
settings >> key;



Answer Source

The statement in main:

void readSettings(ifstream& settings, key, numOfQuestions);

is a function declaration not a function call.

The following is a function call:

readSettings(settings, key, numOfQuestions);

Notice the differences.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download