How to make ofstream global?

Jan 13, 2014 at 4:58am
How to make an ofstream object global so that all class functions can access it?
The example below is my failed attempt.

Passing an instance of type ostream& to func() is not an option, because there will be an intermediary function between main() and func() that I can not change.

Makefile:
1
2
3
4
5
6
7
8
9
10
all: a.exe

a.exe: obj/main1.o obj/A.o
	g++ $^ -o $@

obj/main1.o: main1.cpp
	g++ -Wall main1.cpp -o $@

obj/A.o: A.cpp A.h
	g++ -Wall A.cpp -o $@


main1.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>
#include <iostream>
#include "A.h"
std::ofstream result("result.txt\0");

int main()
{
	A a;

	result << "test1";
	a.func();
}


A.h:
1
2
3
4
5
6
7
8
9
10
#ifndef A_h
#define A_h
#include <fstream>

class A
{
	public:
		void func() { result << "test1"; }
};
#endif 


A.cpp:
1
2
3
#include "A.h"

A::void func() { result << "test1"; }


output:

D:\wolf\Documents\teensy\demo_MinGW\stream>make
g++ -Wall main1.cpp -o obj/main1.o
In file included from main1.cpp:3:0:
A.h: In member function 'void A::func()':
A.h:8:17: error: 'result' was not declared in this scope
   void func() { result << "test1"; }
                 ^
make: *** [obj/main1.o] Error 1


Thank you.
Jan 13, 2014 at 5:33am
First, why do you need to make it global? You can pass it through a constructor, or even as part of the function arguments: (EDIT: Read your question again)
1
2
3
4
5
6
7
8
class A {
public:
    A(std::ofstream& out) : _out(out) {}
    void func(std::ofstream& out) {out << "Hi!"; }

private: 
    std::ofstream& out;
};


Also, in your A.cpp file, you are redefining func. Take your definition out of the header file. In addition, you have some wierd syntax for defining the function, its more normally like this:
 
void A::func(...) {...}


Anyway, as to your question, the same way you make anything global: You declare it as existing outside of the file. Here is how:
1
2
3
4
5
6
7
// A.cpp
#include <ofstream>
#include "A.h"

extern std::ofstream result;

void A::func() { result << "test1"; }
Last edited on Jan 13, 2014 at 5:36am
Jan 13, 2014 at 6:38am
Thanks NT3.
I will pass ofstrem object through the constructor as you suggested. That would be best.

However, I am getting a compile error in main1.cpp.
Please tell me what is causing the compile error.

Makefile:
1
2
3
4
5
6
7
8
9
10
all: a.exe

a.exe: obj/main1.o obj/A.o
	g++ $^ -o $@

obj/main1.o: main1.cpp
	g++ -Wall main1.cpp -o $@

obj/A.o: A.cpp A.h
	g++ -Wall A.cpp -o $@


main1.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "A.h"

int main()
{
	std::ofstream out("result.txt\0");
	A a(out);

	out << "test1";
	a.func(); // undefined reference to `A::func()'
}


A.h:
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef A_h
#define A_h
#include <fstream>

class A
{
	public:
		A(std::ofstream& o) : out(o) {}
		void func();
	private: 
		std::ofstream& out;
};


A.cpp:
1
2
3
#include "A.h"

void A::func() { out << "test1"; }


output:

D:\wolf\Documents\teensy\demo_MinGW\stream>make
g++ -Wall main1.cpp -o obj/main1.o
C:\Users\wolf\AppData\Local\Temp\ccF3djV0.o:main1.cpp:(.text+0x7b): undefined re
ference to `A::func()'
collect2.exe: error: ld returned 1 exit status
make: *** [obj/main1.o] Error 1
Jan 13, 2014 at 6:44am
When you use -o the compiler thinks you want to create the final executable, not an object file.
Jan 13, 2014 at 7:12am
Thanks L B. That fixed it.

Makefile:
1
2
3
4
5
6
7
8
9
10
all: a.exe

a.exe: obj/main1.o obj/A.o
	g++ $^ -o $@

obj/main1.o: main1.cpp
	g++ -Wall -c main1.cpp -o $@

obj/A.o: A.cpp A.h
	g++ -Wall -c A.cpp -o $@
Topic archived. No new replies allowed.