Archived

This topic is now archived and is closed to further replies.

STL string object question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a feeling the answer to this question is easy, which is precisely why I am missing it. I want to use the string class from C++'s Standard Template Library to pass a string to a member function (of my Application class) that will output whatever it is to the open logfile. I thought this would really help me with debugging a fullscreen app, and I bet it will...if I can get the thing to work. =) Here's the deal: I've inline functions declared like this...
#ifdef DEBUG_LOG_FILE_ACTIVE

 inline void OpenLogFile() {ofstream m_LogFile("logfile.txt", ios::trunc)};

 inline void WriteToLog(string LogMessage)
  { m_LogFile << LogMessage << endl; }

 inline void CloseLogFile() { m_LogFile.close(); }

#endif
If remark out my WriteToLog() function, everything compiles 100%, the program runs fine. When I add that one declaration, I get (of all things, the ever embarrassing) syntax error. It's "Syntax Error: String identifier" I tried adding "using namespace std;" to my main.h, but the compiler then said that "no such namespace exists." I'm using Visual C++, so I thought *maybe* Microsoft went so far as to edit the STL itself...but when I looked on MSDN I wasn't able to really gleen much information. Just that both the STL and exist in Visual C++, but apparently MSDN.com doesn't consider an STL member(?!). I could use null-terminated strings, I guess...which if I remember correctly would be something like WriteToLogFile(char *szLogMessage) { m_LogFile << szLogMessage << endl; } ...but I'm at work and, without my system or source code, I can't play with it. So I thought I'd post my little dilemma here and see if anyone could A) tell me why the string class doesn't seem to work in VC++, or B) tell me how they handle logging in their own games. Thanks everyone in advance. EDITTED: I forgot a "}" up above =) [edited by - serratemplar on May 30, 2004 3:53:29 PM] [edited by - serratemplar on May 30, 2004 3:54:01 PM] [edited by - serratemplar on May 30, 2004 3:55:09 PM]

Share this post


Link to post
Share on other sites
I don''t get your problem. Your code compiles fine for me. Anywhoo if your just going to use a c-string, i''d suggest still using strings and change a tiny part of your code from this

inline void WriteToLog(string LogMessage) { m_LogFile << LogMessage << endl; }

to this

inline void WriteToLog(string LogMessage) { m_LogFile << LogMessage.c_str() << endl; }

Share this post


Link to post
Share on other sites
I assume you''ve included string and fstream? You also can''t have the m_LogFile as a local variable of the function OpenLogFile. Try something like this:
ofstream m_LogFile;
inline void OpenLogFile() {m_LogFile.open("logfile.txt", ios::trunc);}


Otherwise, your code looks fine...

Share this post


Link to post
Share on other sites
As TomasH as soon as OpenLogFile returns that file stream will close, i''d suggest turning it an a functor object then you can add more functionality later on:

#include <functional>
#include <fstream>
#include <string>

class logger : public std::unary_function< const std::string&, const bool > {
std::ofstream _ofs;
public:
logger( const std::string& filename,
const std::ofstream::openmode& om = (std::ofstream::out | std::ofstream::trunc)):
_ofs(filename.c_str(), om) {}

const bool operator()(const std::string& msg) {
if(_ofs.good()) {
_ofs << msg;
return true;
} else {
return false;
}
}

~logger() { if(_ofs.is_open()) _ofs.close(); }
};


to use:


#include <cstdlib>

int main() {

logger _logger("log.txt");

_logger("HELLO1");

return EXIT_SUCCESS;
}

Share this post


Link to post
Share on other sites
Thanks for the posts, guys

Yeah, the first thing I looked for was to make sure I'd included string...doesn't seem to like it though? I tried it as string.h too, which also seemed to compile, except for the "string" syntax error. Makes me sad that it compiles for you. That makes me think I have some random compiler option set wrong or something.

m_LogFile is a member variable of the object that currently houses the log file code; I assign it an ofstream...like thus



ofstream m_LogFile;
inline ofstream OpenLogFile(void)
{ ofstream m_LogFile("logfile.txt", ios::trunc)};



I had presumed that m_LogFile would still be in scope after that call as it's not local to that function.

I think, however, that I will create it's own object instead of linking it onto my main application object. We'll see if that straightens it out, I guess. Still doesn't solve why I get a syntax error when I use string.

EDITTED: initially I put the words "string" and "string.h" between lessthan/greaterthan signs...but it used them as tags and thus they didn't show in the post.

[edited by - serratemplar on May 31, 2004 8:37:27 AM]

Share this post


Link to post
Share on other sites
If you do that you create a new ofstream object locally in that function. It will have the same name as the global ofstream. Also, you should put the semicolon inside the function

ofstream m_LogFile;
inline ofstream OpenLogFile() {
m_LogFile.open("logfile.txt", ios::trunc);
}


[edited by - twanvl on May 31, 2004 8:44:10 AM]

Share this post


Link to post
Share on other sites
Ah, of course. =) Thanks for pointing that out.

So it works now, and it is nice to have a logfile. =) It''s helped me find a strange error (entire program crashes when I try to set a rect, which looks okay to me)...if I can''t figure that out I''ll post it elsewhere.

Still no idea why my program won''t allow me to use the string class.

Share this post


Link to post
Share on other sites
quote:
Original post by serratemplar
Ah, of course. =) Thanks for pointing that out.

So it works now, and it is nice to have a logfile. =) It''s helped me find a strange error (entire program crashes when I try to set a rect, which looks okay to me)...if I can''t figure that out I''ll post it elsewhere.

Still no idea why my program won''t allow me to use the string class.


Trying passing a constant reference to the string instead of a by value, also you shouldn''t need to convert it to a c style string. Another thing instead of repeatedly flushing the stream with "endl" use ''\n'' to move start a new line, once your finished with all the messages then flush the stream with endl if you like, so instead of

inline void WriteToLog(string LogMessage) { m_LogFile << LogMessage.c_str() << endl; }


do:


inline void WriteToLog(const std::string& LogMessage) { m_LogFile << LogMessage << ''\n''; }

Share this post


Link to post
Share on other sites