LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2004, 08:49 AM   #1
Zotty
LQ Newbie
 
Registered: Sep 2004
Location: Netherlands
Distribution: Debian testing
Posts: 25

Rep: Reputation: 15
Using streams in combination with classes


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.

Last edited by Zotty; 11-30-2004 at 01:23 PM.
 
Old 11-30-2004, 09:17 AM   #2
michux
Member
 
Registered: Nov 2004
Location: Warsaw, Poland
Distribution: Ubuntu
Posts: 76

Rep: Reputation: 15
It seems fine. You just need to construct an object of class Woot somewhere
Like:

Code:
int main ()
{
        Woot();
        return 0;
}
and it should do.
 
Old 11-30-2004, 01:33 PM   #3
Zotty
LQ Newbie
 
Registered: Sep 2004
Location: Netherlands
Distribution: Debian testing
Posts: 25

Original Poster
Rep: Reputation: 15
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.
 
Old 11-30-2004, 01:59 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.

Last edited by deiussum; 11-30-2004 at 02:10 PM.
 
Old 12-01-2004, 07:24 AM   #5
Zotty
LQ Newbie
 
Registered: Sep 2004
Location: Netherlands
Distribution: Debian testing
Posts: 25

Original Poster
Rep: Reputation: 15
PROBLEM SOLVED

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:

Code:
CBulkadder :: CBulkadder()
{
	invalidHeaderStream.open( "invalid headers", std::ios::out )
}
On a side note:
Code:
CODE A:
CBulkadder :: CBulkadder()
	:invalidHeaderStream( "invalid headers", std::ios::out )
{
}
Code:
CODE B:
CBulkadder :: CBulkadder()
{
	invalidHeaderStream( "invalid headers", std::ios::out )
}
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.

Last edited by Zotty; 12-01-2004 at 07:26 AM.
 
Old 12-01-2004, 09:09 AM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql++ in combination with debina :~ freakin'me Linux - Software 0 10-08-2005 06:37 PM
A combination of sound cards devilkin Linux - Hardware 0 05-16-2005 11:29 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
Is it a good combination to use ??? amit_28oct Programming 1 03-28-2004 11:29 AM
Linking when using gcc/f2c combination? chemspecialist Programming 0 05-17-2001 12:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration