ifstream as parameter

G

Gunnar

Hello.

The (stupid) code below does not compile with the message

In method `istream::istream(const istream &)':
/usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/streambuf.h:128:
`ios::ios(const ios &)' is private
Silly.cpp:15: within this context

It's the function call to "foo" that does not work. My question is, what is
it exactly that makes the compiler (g++ 2.95.4) fail on this?
In the above mentioned file it says:

class ios : public _ios_fields {
ios& operator=(ios&); /* Not allowed! */
ios (const ios&); /* Not allowed! */

What does this mean? The assignment operator and CC is not allowed, but how
can I see that??

(It works fine if I use void foo(ifstream& fil) instead).

-------------------------------------
#include <fstream>
#include <iostream>
using namespace std;

void foo(ifstream fil){
fil.close();
}

int main(){
ifstream fil;
fil.open("hihi");
if (fil.is_open())
cout<<"File open"<<endl;
foo(fil);
if (fil.is_open())
cout<<"File open"<<endl;
}
 
R

Rolf Magnus

Gunnar said:
Hello.

The (stupid) code below does not compile with the message

In method `istream::istream(const istream &)':
/usr/lib/gcc-lib/i386-linux/2.95.4/../../../../include/g++-3/streambuf.h:128:
`ios::ios(const ios &)' is private
Silly.cpp:15: within this context

It's the function call to "foo" that does not work. My question is,
what is it exactly that makes the compiler (g++ 2.95.4) fail on this?
In the above mentioned file it says:

class ios : public _ios_fields {
ios& operator=(ios&); /* Not allowed! */
ios (const ios&); /* Not allowed! */

What does this mean?

Streams are not copyable.
The assignment operator and CC is not allowed, but how can I see
that??

Ehm, you see it in form of a compiler error message if you try to copy a
stream.
(It works fine if I use void foo(ifstream& fil) instead).

If you pass an object by reference, it is not copied.
 
G

Gunnar

class ios : public _ios_fields {
Streams are not copyable.
Well, yes, it says so, but what is it that makes it uncopyable?

How can I write my own class
class Foo{
int number;
};

and make it forbidden to copy?

Foo& (const Foo&);

seems ok to me, but perhaps I've just forgotten about CC's?
 
B

Buster

Gunnar said:
Well, yes, it says so, but what is it that makes it uncopyable?

How can I write my own class
class Foo{
int number;
};

and make it forbidden to copy?

Foo& (const Foo&);

seems ok to me, but perhaps I've just forgotten about CC's?

Make the copy constructor and assignment operator private (like in
the "ios" example - remember members are private by default if you
declare your class with "class"). If you write any constructors you
don't get the implicit default constructor, so make sure you write
at least one accessible constructor or you won't be able to create
any objects of this class.

Regards
Buster
 
G

Gunnar

Make the copy constructor and assignment operator private
Ah! That was a clever solution!
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top