To pass an ifstream type as an argument for a function?

bool initGame(Game &game, int argc, char *argv[]){
	
	
	ifstream board(argv[1]); // <------------------- This part
	ifstream deck(argv[2]);
	int numPlayers= atoi(argv[3]);
	int numRounds= atoi(argv[4]);
	
	if (board.fail() || deck.fail()
		|| numPlayers==0 || MAXPLAYERS < numPlayers
		|| numRounds < 1 || numRounds > 1000
		|| (strcmp(argv[5], "yes") && strcmp(argv[5], "no")))
		{
		cout<< "Improper arguments." << endl;
		exit(1);}
	
	else{
	
	initBoard(board); // <------------ This part
	return true;}
	
	}

I was going to use a separate function to read the data from the file as needed, but I get compiling errors if I try this:

void initBoard(ifstream board){
	string squares;
	getline (board, squares, '\n');
	
	cout << squares;}<------------ For testing purposes, will do other stuff l8r

Any help would be appreciated, as I'm considering taking some very very drastic coding measures to get this to work if I cannot figure it out soon. And really, nobody wants that.

Recommended Answers

All 16 Replies

you have to pass fstream objects by reference

void initBoard(ifstream& board){
	string squares;
	getline (board, squares, '\n');
	
	cout << squares;}<------------ For testing purposes, will do other stuff l8r

I'm not sure if it's due to my Linuxness, but every time I try and compile with that code I get strings of errors, such as

THIS ISN'T CODE THESE ARE REALLY ERRORS

/usr/include/c++/4.2/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.2/iosfwd:55: error: within this context
/usr/include/c++/4.2/iosfwd: In copy constructor ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’:

and

/usr/include/c++/4.2/iosfwd:89: note: synthesized method ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’ first required here 
p3.cpp: In function ‘bool initGame(Game&, int, char**)’:
p3.cpp:45: note: synthesized method ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’ first required here 
p3.cpp:45: error:   initializing argument 1 of ‘void initBoard(std::ifstream)’
p3.cpp: In function ‘void initBoard(std::ifstream&)’:
p3.cpp:53: error: cannot convert ‘std::ifstream*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’

Also my header files

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <sstream>
#include <string>
#include "p3.h"

One of your errors is that the compiler thinks you're trying to call getline(char** lineptr, size_t *n, FILE *stream) (which you can read about with man getline ), instead of the iostreams getline.

Your arguments are out of order; it should be getline(squares, board, '\n') .

>error: cannot convert ‘std::ifstream*’ to ‘char**’ for
>argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
That looks like your compiler is trying to call the wrong function. Try prefixing getline with std:: to force name lookup to check the std namespace.

void initBoard(ifstream& board){
	string squares;
	std::getline (board, squares);
	
	cout << squares;}

That is my entire function, and it's still throwing some pretty wild errors (even if I try and take out the cout and leave only the important code)

Did you... read my post?

Did you... read my post?

Yes, even with the arguments reversed I'm still getting

cjtrombl@cjtrombl-laptop:~/Documents/programs/proj3$ g++ -Wall -Werror -m32 p3.cpp p3support.cpp -o test
/usr/include/c++/4.2/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.2/iosfwd:55: error: within this context
/usr/include/c++/4.2/iosfwd: In copy constructor ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2/iosfwd:89: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
/usr/include/c++/4.2/streambuf: In copy constructor ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2/streambuf:794: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/include/c++/4.2/iosfwd:86: error: within this context
/usr/include/c++/4.2/iosfwd: In copy constructor ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2/iosfwd:89: note: synthesized method ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’ first required here 
p3.cpp: In function ‘bool initGame(Game&, int, char**)’:
p3.cpp:45: note: synthesized method ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’ first required here 
p3.cpp:45: error:   initializing argument 1 of ‘void initBoard(std::ifstream)’
p3.cpp: In function ‘void initBoard(std::ifstream&)’:
p3.cpp:53: error: cannot convert ‘std::string’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
cjtrombl@cjtrombl-laptop:~/Documents/programs/proj3$ 



void initBoard(ifstream& board){
	string squares;
	getline(squares, board, '\n');}
initBoard(board);




void initBoard(istream& board){
	string squares;
	getline(board, squares, '\n');
	cout << squares;}

istream vs ifstream: one of them works and the other does not.

>>Istream vs ifstream: one of them works and the other does not.

Yes -- but do you know which one is the correct one ? (Hint: check spelling and capatilization).

The capitalization was an accident.

For whatever reason I can pass board as an istream but NOT as an ifstream.

There is something else wrong. Post complete code. If its pretty large then hit the Go Advanced button and mnake it an attachment.

There is something else wrong. Post complete code. If its pretty large then hit the Go Advanced button and mnake it an attachment.

Hopefully it's not too long.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <sstream>
#include <string>
#include "p3.h"



bool initGame(Game &game, int argc, char *argv[]);
void initBoard(istream& board, Game& game);


using namespace std;

int myMain(int argc, char *argv[]){ <----- This is a required function in my coding project
	
	struct Game game;
	initGame(game, argc, argv);
	return 0;

	
	
}


bool initGame(Game &game, int argc, char *argv[]){
	
	
	ifstream board(argv[1]);
	ifstream deck(argv[2]);
	int numPlayers= atoi(argv[3]);
	int numRounds= atoi(argv[4]);
	
	if (board.fail() || deck.fail()
		|| numPlayers==0 || MAXPLAYERS < numPlayers
		|| numRounds < 1 || numRounds > 1000
		|| (strcmp(argv[5], "yes") && strcmp(argv[5], "no")))
		{
		cout<< "Improper arguments." << endl;
		exit(1);}
	
	else{
	
	
	initBoard(board, game);
	return true;}
}
	
	
void initBoard(istream& board, Game &game){
	string square;
	getline(board, square, '\n');
	game.board.numSquares= atoi(square.c_str());
	cout << game.board.numSquares << endl;}

The problem is with the highlighted text.

just change lines 12 and 52 to use ifstream instead of istream. And since you did not code the using namespace std; you have to use std::ifstream ... to specify the std namespace.

just change lines 12 and 52 to use ifstream instead of istream. And since you did not code the using namespace std; you have to use std::ifstream ... to specify the std namespace.

Line 15. I always code using namespace std;, I wouldn't know what to do without it.

Oh I see that now. Move line 15 up to line 9 because lines 11 and 12 need that.

Wow. Sometimes I just hate me. That was the only reason it was failing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.