I want to do somthing like this.

ifstream My_File::GetFile_Stream() const
{
      return(this->Input_Stream);
}

where Input_Stream is a private member of the class My_File and is declared as follows:
ifstream Input_Stream;

When I compile I get the following errors:

/usr/include/c++/3.3.1/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++/3.3.1/bits/ios_base.h:668: error: `
std::ios_base::ios_base(const std::ios_base&)' is private
My_File.cpp:25: error: within this context
/usr/include/c++/3.3.1/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++/3.3.1/streambuf:922: error: `std::basic_streambuf<_CharT,
_Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&)
[with _CharT = char, _Traits = std::char_traits<char>]' is private
My_File.cpp:25: error: within this context

I don't understand the error message either.

Thanks for any help.

Post a small code where you are using your function, please

I think you might have to return a reference/pointer to the stream. Though if it's a local variable you might have to return a dynamically allocated stream.

#ifndef FILEHANDLER_H_
#define FILEHANDLER_H_

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class FileHandler
{
public:
	FileHandler(const string &);
	~FileHandler();
	string getFileName()const;
	void setFileName(const string &);
	ifstream getFileIn()const;
	ofstream getFileOut()const;
	bool openFileToRead();
	bool openFileToWrite();
	string readLine();
	void writeLine(const string &);
	void closeInputFile();
	void closeOutputFile();

private:
	string fileName;
	ifstream fileIn;
	ofstream fileOut;
};

#endif /*HANDLER_H_*/

I wanted to create my own file handling class,but

ifstream FileHandler::getFileIn()const{
	return fileIn;
}
ofstream FileHandler::getFileOut()const{
	return fileOut;
}

but these two functions make difficulties..please help..

Like twomers said, just try to return them as references:

ifstream& FileHandler::getFileIn()const{
return fileIn;
}

Like twomers said, just try to return them as references:

ifstream& FileHandler::getFileIn()const{
return fileIn;
}

yeah I tried it and it gives no compiling error

but when I want use it in such a loop

string line;
while(getline(pFileHandlerObj->getFileIn(),line)){
}

it doesnt work,
while this one works if we asssume it is not a private object

string line;
while(getline(fileIn,line)){
}

and sorry but this gives error too

ifstream& FileHandler::getFileIn()const{
return fileIn;
}

compile error:
invalid initialization of reference of type 'std::ofstream&' from expression of type 'const std::ofstream'

class FileHandler
{
public:
	FileHandler(const string &);
	~FileHandler();
	string getFileName()const;
	void setFileName(const string &);
	ifstream& getFileIn()const;
	ofstream& getFileOut()const;
	bool openFileToRead();
	bool openFileToWrite();
	string readLine();
	void writeLine(const string &);
	void closeInputFile();
	void closeOutputFile();

private:
	string fileName;
	ifstream &fileIn;    // ref
	ofstream &fileOut; // ref
};

I made changes above but this times it gives constructor error

/* constructor takes the filename as parameter */
FileHandler::FileHandler(const string &fileName){
	setFileName(fileName);
}

errors:
uninitialized reference member `FileHandler::fileIn'
uninitialized reference member `FileHandler::fileOut'

how can I initalize them?

and sorry but this gives error too

ifstream& FileHandler::getFileIn()const{
return fileIn;
}

compile error:
invalid initialization of reference of type 'std::ofstream&' from expression of type 'const std::ofstream'

Huh? ifstream function cannot give ofstream error.
Be sure that you've changed declarations in header file too!

And no, you don't need to make private variables references. Only the function returns reference

I think you might have to return a reference/pointer to the stream. Though if it's a local variable you might have to return a dynamically allocated stream.

no,as you see its not local varable,its class member,how can I do that?

Huh? ifstream function cannot give ofstream error.
Be sure that you've changed declarations in header file too!

And no, you don't need to make private variables references. Only the function returns reference

.
.
private:
	string fileName;
	ifstream fileIn;
	ofstream fileOut;
.
.

ifstream& FileHandler::getFileIn()const{
	return fileIn;
}
ofstream& FileHandler::getFileOut()const{
	return fileOut;
}

okay,I corrected it as you said
the errors still remain:

invalid initialization of reference of type 'std::ifstream&' from expression of type 'const std::ifstream'
invalid initialization of reference of type 'std::ofstream&' from expression of type 'const std::ofstream'

oh,I found where I was wrong

these member functions cant be const ,right?

ifstream& FileHandler::getFileIn(){	
         return fileIn;
}

ofstream& FileHandler::getFileOut()
{	
         return fileOut;
}
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.