ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm having trouble using (file)streams in combination with classes. Let's say we have the following code;
Code:
ofstream invalidHeaderStream( "invalid headers", ios::out );
int main()
{
// write something to the stream. This will happen thousands of times in the actual app.
function( "Blaat" );
// close stream at the end
invalidHeaderStream.close();
return 0;
}
void function( string textline )
{
invalidHeaderStream << textline << endl;
}
The reason the stream is declared globaly is that my program does a lot of writing during the entire run. Seems to me it's not efficient to open and close the file 10.000 times 8-)
As I'm using classes now, streams are the only thing I haven't figured out yet. It should be something like this;
Code:
class Woot {
public:
Woot();
~Woot();
private:
/* POINT A */
ofstream invalidHeaderStream(); // declare stream here (this compiles)
void function( string textline );
};
Woot::Woot()
{
/* POINT B */
// open the stream (to a textfile) - as in "invalidHeaderStream( "invalid headers", ios::out );"
}
Woot::~Woot()
{
/* POINT C */
// close the stream - as in "invalidHeaderStream.close();"
}
void Woot::function( string textline )
{
invalidHeaderStream << textline << endl;
}
So far I haven't been able to get it working. Any help would be appreciated.
But offcourse the class object is constructed somewhere, in main() to be precise
In fact the entire program compiles and is fully operational in 'OO-mode' as long as the stream code is outcommented. In the above code I've added 3 points (A, B and C) to clarify where the problem lies. I've tried to bluntly guess the correct code on those points, but the only thing that GCC accepts is the line at point A. The other 2 lines just output various lines of compile errors.
Maybe I'm not sure what you are asking exactly, but if you want to call the constructor of the ofstream in the constructor of your Woot class with the parameters you choose, you can use a constructor list something like so:
Code:
class Woot
{
public:
Woot();
private:
ofstream m_oStream; // NOTE: no (), putting the ()
// would cause it to be interpreted as a
// function taking no parameters and
// returning an ofstream object...
};
Woot::Woot()
:m_oStream("filename", ios::out)
{
}
Although, I think that the main problem you are seeing with the code you posted is that you aren't actually declaring an ofstream object, you are declaring a function that returns an ofstream object...
If you still have problems compiling, try posting exact errors.
That was exactly the kind of answer I was looking for :) The problem is solved now. It took another 15 minutes of fiddling around, but you put me on the right track. Thanks for the replies.
The solution:
The () needed to be removed offcourse. Stupid mistake. I also included 'iostream' instead of 'fstream'. Whilst fstream inherits from iostream, it doesn't work for obvious reasons.
In the constructor stream.open() needs to be used:
The method used in code A compiles, code B does not and causes a "error: no match for call to `(std::ofstream) (const char[16], const std::_Ios_Openmode&)'". However, executing the first will cause a segmentation error and kills the program.
The second method doesn't compile because you can't call the constructor on an already constructed object, and by the time it gets inside the block, that object has already been constructed using the default constructor. The constructor initialization list is how you can specify the constructor for member objects of your class.
Not quite sure why you are seeing a segmentation fault using that, though. It could be related to some other part of your code that isn't being shown, though. Something like accessing an array beyond it's bounds corrupting memory in another part of the program for example... Stuff like that can be a boat load of fun to track down.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.