Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to declare a static fstream within a class?

Reply
Thread Tools

How to declare a static fstream within a class?

 
 
Javi
Guest
Posts: n/a
 
      10-10-2005
Hi!. I'm trying to do something like this but results in lot of errors.
Can anybody tell me how to do it (first of all I hope you can grasp my
idea)?:

myfile.cpp
#include <iostream>
#include <fstream>

using namespace std;

class debuginformation{

public:

static fstream outfile;
static void message001(int n);
static void message002(double alpha);
};

fstream debuginformation:utfile("debug.dat"); //Is this rigth
// Maybe I should write fstream
debuginformation:utfile=fstream("debug.dat");?

void debuginformation::message001(int n){outfile<<"The var n
is"<<n<<endl;}


int main(){
// Do I have to declare any objects of class debuginformation?
// I only need to write the various static messages to the file
outfile.

int n=10;
debuginformation::message001(n);

}


If I compile this I get lot of errors complaining about:

undefined reference to `std::basic_ostream<char,
std::char_traits<char> >& std:perator<< <std::char_traits<char>
>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'


In methods involving this structure: outfile << anything.


Well I hope you can understand what I mean, maybe I've not been very
clear.

Thanks,

Javi.

 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      10-10-2005
On 10 Oct 2005 04:03:08 -0700, "Javi" <(E-Mail Removed)>
wrote:

>Hi!. I'm trying to do something like this but results in lot of errors.
>Can anybody tell me how to do it (first of all I hope you can grasp my
>idea)?:
>
>myfile.cpp
>#include <iostream>
>#include <fstream>
>
>using namespace std;
>
>class debuginformation{
>
>public:
>
>static fstream outfile;
>static void message001(int n);
>static void message002(double alpha);
>};
>
>fstream debuginformation:utfile("debug.dat"); //Is this rigth
>// Maybe I should write fstream
>debuginformation:utfile=fstream("debug.dat"); ?
>
>void debuginformation::message001(int n){outfile<<"The var n
>is"<<n<<endl;}
>
>
>int main(){
>// Do I have to declare any objects of class debuginformation?
>// I only need to write the various static messages to the file
>outfile.
>
>int n=10;
>debuginformation::message001(n);
>
>}
>
>
>If I compile this I get lot of errors complaining about:
>
> undefined reference to `std::basic_ostream<char,
>std::char_traits<char> >& std:perator<< <std::char_traits<char>
>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

>
>In methods involving this structure: outfile << anything.
>
>
>Well I hope you can understand what I mean, maybe I've not been very
>clear.
>
> Thanks,
>
>Javi.


Once I fixed up the line endings, and added an implementation for
message002(double), your code compiled fine with the Borland 5.5.
command-line compiler. Is this the exact same code you are having
trouble with?

--
Bob Hairgrove
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Javi
Guest
Posts: n/a
 
      10-10-2005
Yes, my exact code is:

#include <iostream>
#include <fstream>

using namespace std;

class dbginfo{

public:
static fstream outfile;
static void d001(int n);
static void d002(double d);
};

fstream dbginfo:utfile("output.dat");
void dbginfo::d001(int n){outfile<<"First proof"<<n;}
void dbginfo::d002(double d){outfile<<"First proof"<<endl;}

int main(){
int n=10;
dbginfo::d001(n);}

Simple enough, but I do have some compilation problems with gnu g++.

OK,OK,OK...well I'm so embarrassed... It was a problem with my .o code,
I didn't remove it after a first failed compilation and afterwards the
make program took this bad code.

Thanks a lot Bob for your help and for taking some time to check my
code, I was going crazy.

Thanks again,
Javi.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
question re. usage of "static" within static member functions of aclass ssb C++ 58 09-14-2009 03:09 PM
Why can't i declare a static variable in a static method? ZelluX Java 24 11-19-2007 11:58 AM
How to forward declare classes within namespace? Divick C++ 9 11-08-2005 07:39 PM
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM



Advertisments