error: 'ostream' has not been declared

W

wyndz0108

hello,
i have this program that prints out some text using ostream. i am
using g++ (GCC) 3.4.4; the original program compiles and run. refer to
first program below.
but when i modified it by moving the class to a header file, i
encountered an error. refer to second program below.
can somebody help?


// start of original program
#include <fstream>
#include <iostream>

using namespace std;

class outC {
public:
void display (ostream& out) { out << "testing 1, 2, 3" << endl;}

};

int main (int argc, char * argv) {
ofstream outfile;
outfile.open("out.tmp");

outC my;
my.display(outfile);

outfile.close();
return 0;
}

// end of original program

modified program with 2 files (main and header files)

//start of main file (modified program)
#include <fstream>
#include <iostream>
#include "out.h"

using namespace std;

int main (int argc, char * argv) {
ofstream outfile;
outfile.open("out.tmp");

outC my;
my.display(cout);

outfile.close();
return 0;
}

void outC::display (ostream& out) {
out << "testing 1, 2, 3" << endl;
}

// end of main file (modified program)


//start of header file (for the modified program)
#ifndef OUTH
#define OUTH

class outC {
public:
void display (ostream& out);

};

#endif

// end of header file (for the modified program)
 
S

Steve.M.Brain

Hi,

the most likely reason it wont compile is that your

using namespace std;

line should be above the
#include "outC.h" header
(or in the outC.h file itself)
 
S

Salt_Peter

hello,
i have this program that prints out some text using ostream. i am
using g++ (GCC) 3.4.4; the original program compiles and run. refer to
first program below.
but when i modified it by moving the class to a header file, i
encountered an error. refer to second program below.
can somebody help?

// start of original program
#include <fstream>
#include <iostream>

using namespace std;

class outC {
public:
void display (ostream& out) { out << "testing 1, 2, 3" << endl;}

};

int main (int argc, char * argv) {
ofstream outfile;
outfile.open("out.tmp");

outC my;
my.display(outfile);

outfile.close();
return 0;

}

// end of original program

modified program with 2 files (main and header files)

//start of main file (modified program)
#include <fstream>
#include <iostream>
#include "out.h"

using namespace std;

int main (int argc, char * argv) {
ofstream outfile;
outfile.open("out.tmp");

outC my;
my.display(cout);

outfile.close();
return 0;

}

void outC::display (ostream& out) {
out << "testing 1, 2, 3" << endl;

}

// end of main file (modified program)

//start of header file (for the modified program)
#ifndef OUTH
#define OUTH

#include said:
class outC {
public:
void display (ostream& out);

void display(std::eek:stream& out);
 
W

wyndz0108

#include <ostream>




          void display(std::eek:stream& out);






- Show quoted text -- Hide quoted text -

- Show quoted text -

thank you guys... it's compiling now.
 
P

Pete Becker

the most likely reason it wont compile is that your

using namespace std;

line should be above the
#include "outC.h" header
(or in the outC.h file itself)

Don't mess with namespaces in somebody else's header file. Using
declarations should come after all header includes. Otherwise the
header ends up seeing symbols it may not be prepared for.
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top