473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

ifstream as parameter

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;
}

Jul 22 '05 #1
4 2837
Gunnar wrote:
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.

Jul 22 '05 #2
>> class ios : public _ios_fields {
ios& operator=(ios&); /* Not allowed! */
ios (const ios&); /* Not allowed! */

What does this mean?

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?
Jul 22 '05 #3

"Gunnar" <gu***@comhem.se> wrote
class ios : public _ios_fields {
ios& operator=(ios&); /* Not allowed! */
ios (const ios&); /* Not allowed! */

What does this mean?

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?


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
Jul 22 '05 #4
> Make the copy constructor and assignment operator private
Ah! That was a clever solution!
Thanks!
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: ragi | last post by:
Short version of my program: ifstream File; File.open("test.txt"); if(!File.good()) return; Func(File); <--cannot convert parametr 1 from 'std::ifstream' to 'std::istream'
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
3
by: toton | last post by:
Hi, I want to unread a few things while I am reading a file. One solution I know is putback the characters read to the buffer. But as I need frequent moving file pointer , to a few steps back, I...
4
by: serleon | last post by:
Privet/hello/salut/halo/hola! I have the following code: ________________________________________________________________ #include "OJFKinematics.h" #include "OJFJets.h" #include "OJFSearch.h"...
3
by: Leo | last post by:
Privet/hello/salut/halo/hola! I have the following code: #include <iostream> #include <iomanip> #include <fstream> #include <cstdlib> using namespace std; using namespace Finder;
4
by: supemoy | last post by:
I am having a heck of a time using the ifstream::read() method. I am using Visual Studio 6.0, and my intelisense indicates that read has an overload of: ifstream::read(unsigned char*, int _n) A...
12
by: Kira Yamato | last post by:
I've posted this in another thread, but I suppose I should've started a new thread for it instead. I cannot get the following short program to compile under g++: #include <iostream> #include...
4
by: Chris Forone | last post by:
hello group, why have i to bracket the second ctor param in the following example? thx & hand, chris #include <fstream> #include <iterator> int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.