error with string ><

Ok so i keep getting this error:

Error 1 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream 890 1 program1B

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream getfile(ifstream);

const int k=5;
void main(){
	//more or less does nothing :]
	ifstream infile;
	string words[k];
	cout<<"welcome to the Word Finder Generator";
	getfile(infile);
	return;
}

ifstream getfile(ifstream words){
	//gets the file name from the user, opens it, and returns to the main
	string filename;
	do{
	cout<<"Please enter the name of the file with the word list: ";
	cin >> filename;  // dont understand why this is not working?
	words.open(filename);
	if (words.fail())
		cout << "I'm sorry, that file could not be found. Please try again.\n";
	}while(words.fail());
	return (words);
}


above is my code I'm not a very strong programmer and it's really frustrating to run into a problem this early on can you see anything wrong that i've done?
open() takes a C-string. You want words.open(filename.c_str())
hey thanks a lot i originally had included <cstring> would that have fixed this error also?
Last edited on
No. A C-string just means a null-terminated character array, as opposed to C++ style std::string.
I fixed it like you showed me but i still get the same error... i am wondering if it is just compiling the code before i changed it visual studio does that to me a lot...
Ok, I've narrowed the problem down, and it seems you aren't allowed to return an ifstream the way you are. If I were you, I would simply pass words as a reference to an ifstream.

Edit: Also, I didn't notice this before, but you shouldn't use void main(). Use int main() instead.
Last edited on
your function getfile returns an ifstream but in your code you never set anything equal to that return value. To illustrate what I'm talking about...

you did
getfile(infile);
what I suggest you should have done is something along the lines of
infile = getfile(infile);

The diference is that the return value of getfile is stored somewhere, whereas the way you did it the return value is just dropped.

Zhuge brings up a good point that you should really be passing your ifstream by reference.
What your posted code is doing is taking in an ifstream called words, making a copy of it, making some changes to that copy and finally returning the changed version of words.
If you change your function to pass by reference with a void return value it will take in the actual ifstream and make changes directly to it so that when your function has finished the changes will have been made.

pass by value with a return value
1
2
3
ifstream getfile(ifstream words){
...
return;}


pass by reference with no return value
1
2
3
void getfile(ifstream& words){
...
}


This is a concept beginner c++ programmers have a really hard time wrapping their head around.
Read this: http://www.tech-recipes.com/rx/1232/c-pointers-pass-by-value-pass-by-reference/

I was once you! Keep on Givin' 'er!
Last edited on
Actually this makes a ton of sense i just like to ignore the fact that i can pass by reference because i always end up changing things that ought not be changed. thanks for the help!
Just to confirm this did work and i do actually have an understanding of why. It is just a matter of being able to see this on my own that i fail at.

On to other modules!!
Last edited on
My code now is as follows
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
//creates a 10 by 10 word search puzzle
//words determined by a specified filed
//word locations then specified by user
//program creates random letters to fill in the puzzle
//programmer : Tyler Sievers
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void getfile(ifstream&);
void populatearray(ifstream, string[]);
void testfcn1(string[]);

const int k=5;    //most words allowed in file constant for the first array
void main(){
	//more or less does nothing :]
	ifstream infile;
	string words[k];
	cout<<"welcome to the Word Finder Generator\n\n";
	getfile(infile);
	populatearray(infile, words);
	testfcn1(words);
	return;
}

void getfile(ifstream& words){
	//gets the file name from the user, opens it, and returns to the main
	string filename;
	do{
	cout<<"Please enter the name of the file with the word list: ";
	cin >> filename;
	words.open(filename.c_str());
	if (words.fail())
		cout << "I'm sorry, that file could not be found. Please try again.\n";
	}while(words.fail());
	return;
}

void populatearray(ifstream file, string word[])
{//takes the array words and fills it with the words from the file!!
	int i; //array counter
	for(i=0; i<k; i++)
		file>>word[i];
	return;
}

void testfcn1(string word[]){
	int i;
	for(i=0;i<k;i++)
		cout<<word[i];
	return;
}


i am getting the same error i was getting before and i dont understand why testfcn1 is just to test that my fill array function is working properly and will be commented out when i confirm that it is... any idea why now all of a sudden after the ifstream was working properly it has decided not to again?
heavy sigh... and when i try to debug step by step it tells me there is no source code available... fml i really wish i wasnt required to use msvs
maybe i dont understand why i have to pass the ifstreams by reference but at least now i realize that i HAVE to
closed account (zwA4jE8b)
instead of passing an ifstream as the variable pass a string and in main do something like
1
2
3
4
5
6
7
8
9
10
void main(){
     //more or less does nothing :]
     ifstream infile;
     string filename;
     do{
          get filename from function
          infile.open(filename.c_str());
          }
     while (infile.fail());
     


also you are using a return in a void function!?
Last edited on
@TC: When you are stepping through, generally use Step Over unless you are going into a function of your own making. Otherwise you may end up trying to step into a system/STL/etc function, which in some cases may be in a library or somewhere where you don't have the source code available.

As for the ifstream portion, I have to confess I'm not certain why you can't return it. I originally thought you might simply not be able to copy them, but that doesn't seem to be the issue.
Error 1 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

I believe the private member referenced in the error message is the assignment operator or copy constructor which are private to prevent copying and assignment. Copying the internal data would result in the pointers to the stream buffer being copied and hence the two streams would share the same stream buffer. Here is a link with an explanation: http://stdcxx.apache.org/doc/stdlibug/34-2.html

You need to use references.
Sieves, unless your assignment says that you NEED to use functions then I would recommend not using them here. Realistically, the only time you should use a function is when you know that various pieces of your code will be doing the same thing in different places. There seems to be some kind of problem with the way your are passing values to a function and the way you are calling that function. Why not tackle this problem by starting out with no functions and see if you can get that to work first. Then if it's part of your assignment once you have code that works break it down into functions.

...anyway, since your functions will only be called once they are kind of useless as functions, they might as well be blocks of code. That is my point.

...just a thought lol
Topic archived. No new replies allowed.