1

I have more than one sources files and header files. They are;

main.cpp tools.cpp hh.cpp fitness.cpp

tools.h hh.h fitness.h

I want to define an ofstream in the tools.h The tools.h is included in the all other sources files. But when I add the std::ofstream myOutput; into the tools.h, it says "multiple definition of myOutput". However, there is not any other line about the myOutput variable. If I can define it properly, then I want to do myOutput.open("observe.txt",std::ofstream::app);.

  • A relevant snippet of code would be most helpful. – shauryachats Feb 09 '15 at 09:54
  • @ShauryaChats here is my code: http://stackoverflow.com/questions/28377741/sigabrt-in-arrays-c-how-to-turn-to-vector/28377923?noredirect=1#comment45105668_28377923 –  Feb 09 '15 at 09:55
  • 2
    Have `extern std::ofstream myOutput;` in tools.h, and `std::ofstream MyOutput;` in tools.cpp. Make sure they're enclosed by the same `namespace xyz { }` blocks (if applicable), or add the namespace as a prefix in the .cpp ala `std::ofstream myNamespace::MyOutput;` That said, it's effectively a global variable which is a bit of a code smell - if possible create it as a local stack-hosted variable in the function that needs it, though that may be impractical for something like an app-wide log file. – Tony Delroy Feb 09 '15 at 09:56
  • @TonyD thank you. It worked. I need it just to collect some data. Then I will remove it. So it is not important if it is global or local. thanks again. –  Feb 09 '15 at 09:59
  • @TonyD I think something is wrong. I did what you said, but I have to use this variable in the other sources files. So please tell me a way to declare the `myData.txt` in globally and use it in all the sources files. I mean, for example, I'd like to type `myOuput<<"hello world"< –  Feb 09 '15 at 10:10
  • @Whcrs: that should already be possible as long as main.cpp has `#include "tools.h"`, so it sees the "`extern..`" statement and knows about `myOutput`. If you are still having trouble, please tell us the exact error message you're getting. – Tony Delroy Feb 09 '15 at 10:15
  • @TonyD you didn't understand. I tell you Where to I need to declare `myOutput.open("observe.txt",std::ofstream::app);` globally. Then I wanna call `myOutput<<"hello"< –  Feb 09 '15 at 10:18
  • You're right - I didn't understand that was what you meant. Ok - you need to do that before you use the stream: near the start of `main()` is an obvious place to do it; if there's some function in `tools.cpp` that you call before attempting output, that might be a reasonable option too; another option is to change the definition in tools.cpp to `std::ofstream myOutput("observe.txt");`, then the constructor - which is guaranteed to be called before `main()` starts running, will open the stream itself (then you'll need to make the variable an `ofstream` in tools.h too). – Tony Delroy Feb 09 '15 at 10:22
  • @TonyD -- condense those comments into an answer, and I'll upvote it :) – LThode Feb 09 '15 at 21:46

1 Answers1

0

Put extern std::ofstream myOutput; in tools.h, and std::ofstream MyOutput; in tools.cpp. Also, put #include <fstream> in tools.cpp. To not use std:: enter the line using namespace std; in both files. The code to open the file is in main.cpp.

Willy
  • 61
  • 1
  • 1