Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,544 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,541 people online right now. Registration is fast and FREE... Join Now!




A file stream as a member of a static class

 
Reply to this topicStart new topic

A file stream as a member of a static class, Another quick and probably stupid question..

Imek
29 Oct, 2007 - 09:41 AM
Post #1

D.I.C Head
**

Joined: 25 Oct, 2007
Posts: 52


My Contributions
Hey,

I've been running into problems with this class I'm writing for file I/O. As I understand it, a stream is just a class - so I can do pointers with it like below, right? But it's giving me problems. Here are the relevant bits of code:

(File.h)
CODE
#ifndef FILE_H_
#define FILE_H_

#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <GameState.h>

using namespace std;

/**
* This static class does all the file handling, including any necessary parsing of data.
* The two things files are used for are logging of statistics and storage of learned
* data for the learning AI.
*/
class File
{
public:
    // Log file stuff. We only handle the one log file, specified in the Settings class.
    static void OpenLogFile();
    static void WriteLogLine(string strLine);
    static void CloseLogFile();

    // Learning file stuff (chooses file based on player name). Only one open at a time.
    static void OpenPlayerFile(string strName);
    // Get stored Value for a state-action pair.
    static int GetValue(GameState *oState, int iUnit);
    // Change the stored Value for a state-action pair
    static int WriteValue(GameState *oState, int iUnit, int iNewVal);
    static void ClosePlayerFile();
    
private:
    static fstream *fsPlayerFile;
    static ofstream *fsLogFile;
};

#endif /*FILE_H_*/


(File.cpp)
CODE
#import <File.h>

void File::OpenLogFile()
{
    fsLogFile = new ofstream("fe");
}


The error it gives me is "undefined reference to `File::fsLogFile'" at the line where I try to initialise the log file stream. Am I missing something really obvious here?
User is offlineProfile CardPM
+Quote Post

Pontus
RE: A File Stream As A Member Of A Static Class
29 Oct, 2007 - 09:53 AM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
I once had a similar problem.

Add this to ur File.cpp
CODE
static ofstream File::*fsLogFile;

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: A File Stream As A Member Of A Static Class
29 Oct, 2007 - 11:05 AM
Post #3

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Just to expand on that a little, static members need to be declared in the global scope - it can be in the implementation file outside of the class definition itself, in your main() driver file, etc. But as manhaeve suggested, you need to do with at the global scope but also specify the class scope at the same time.

This page has a short, but pretty good, description of the use of static members.
User is offlineProfile CardPM
+Quote Post

Imek
RE: A File Stream As A Member Of A Static Class
30 Oct, 2007 - 12:31 PM
Post #4

D.I.C Head
**

Joined: 25 Oct, 2007
Posts: 52


My Contributions
Yeah, thanks - I really should have known that.

Just a tiny note - what worked without errors for me was slightly different to the suggestion. Here's what works:

CODE
#import <File.h>

ofstream *File::fsLogFile;

void File::OpenLogFile()
{
    fsLogFile = new ofstream(Settings::strLogPath.c_str());
}



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:27PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month