How to declare a static fstream within a class?

J

Javi

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::eek:utfile("debug.dat"); //Is this rigth
// Maybe I should write fstream
debuginformation::eek: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 said:
(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.
 
B

Bob Hairgrove

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::eek:utfile("debug.dat"); //Is this rigth
// Maybe I should write fstream
debuginformation::eek: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,


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?
 
J

Javi

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::eek: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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top