PDA

View Full Version : problem with pointer-reference



mickey
22nd February 2007, 18:18
class File {
private:
std::fstream file;
...............................
public:
std::fstream open_file(int);
}
std::fstream File::open_file() {
file.open(file_name.c_str(), std::ios_base::in); //read only
return file;
}

int readFile(File& f) {
std::fstream ff = f.open_file(); //EDIT
if (ff) { //EDIT : if I put if (!f.open_file()) is always 0
// I need check if file exists
}

Hi this doens't LINK. anyone could explain me the error? thanks

e8johan
22nd February 2007, 19:22
If you show us the errors we don't have to try to find them in your code. What does your compiler tell you?

jacek
22nd February 2007, 20:13
Try adding a semicolon after the class definition.

mickey
22nd February 2007, 20:22
(semicolon is a cut&past error)
Above I edited the code because there wasn't a a thing.. it isn't LINKING but it compile


microsoft visual studio 8\vc\include\fstream(933) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
microsoft visual studio 8\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
This diagnostic occurred in the compiler generated function 'std::basic_fstream<_Elem,_Traits>::basic_fstream(const std::basic_fstream<_Elem,_Traits> &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

wysota
22nd February 2007, 22:00
I suggest that File::open_file returns a reference to the stream instead of a copy.

mickey
23rd February 2007, 12:38
I suggest that File::open_file returns a reference to the stream instead of a copy.
why? it doens't linking............
It works if I declare std::fstream& ff (ff as reference). Could me explain why?
thanks

wysota
23rd February 2007, 15:33
Because you can't copy streams and you try to do that two times (first you return a copy of the stream from the open function and then you try to assign it to another object, which copies it again. You have to work on references.

mickey
24th February 2007, 19:50
class File {
private:
std::fstream file;
...............................
public:
std::fstream& open_file();
}
std::fstream& File::open_file() {
file.open(file_name.c_str(), std::ios_base::in); //read only
return file;
}

int readFile(File& f) {
std::fstream* ff = f.open_file(); // here I used '*', heere ERROR
if (ff) {
........................
}

Hi I changed it but doens't compile. Now ff isn't a copy! Compiler says that I cant convert
fstream into fstream*. Why? Isn't f.open_file returing a 'reference'???

wysota
24th February 2007, 20:10
int readFile(File& f) {
std::fstream* ff = f.open_file(); // here I used '*', heere ERROR
if (ff) {
........................
}

ff is now a pointer to std::fstream and not a reference...

Try this:

std::fstream &ff = f.open_file();

mickey
24th February 2007, 22:45
hi, I just tried it and goes well but I'm finding to understand this:
my function open_file return a reference so I do fstream* pfile = open_file() because I always do this eg: int j; int* pint = &j (your suggest for me is something like

int j; int& pint = &j
And this above doens't compile..
So the returned reference of open_file for me is like &j. Isn't that? or what?
thanks...

wysota
24th February 2007, 22:59
"int &" is a "reference to int", whereas int* is a "pointer to int". These are types. Now we also have a "dereference operator" (*) which is a unary operator that returns an object pointed by its argument and an "address operator" (&) which is a unary operator that returns an address of its argument. So "&j" is something completely different than "int&". Same goes for asterisks.

I think it's high time for you to download and read Thinking in C++ (http://mindview.net/Books/TICPP/ThinkingInCPP2e.html).