Static std::ostream objects

This topic is 2827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have a debug utility class that I use instead of cout. I do this because I want to be able to quickly switch between outputting to console, files, network, GUI, etc. All I would have to do would be set the ostream object up so that it's streambuff points to either a file, or cout. I already figured out how to attach a stream to cout. The problem is, I get undefined reference errors in Visual C++ when I try to declare a static ostream object. This is my class declaration:

 ///@brief The DebugUtils class contains debugging functions for outputting strings to the console, or files. class DebugUtils { public: ///@brief This stream can be used instead of std::cout. static std::ostream out; ///@brief The BaseGameApp class is a friend so that it can acces this class's constructor. friend class BaseGameApp; private: ///@brief The DebugUtils constructor is only called by the game application. DebugUtils(); }; 

How do I prevent undefined references to the DebugUtils::out? I have the constructor of that class being called by the main game app's constructor, so the debugging utilities can be used after creating an app. That's where I connect it to std::cout.

Share on other sites
The part inside the class definition is just a declaration. You need to provide a definition in addition to the declaration. In this case it would look like:
 std::ostream DebugUtils::out; 

Share on other sites
Thanks. It worked.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12