Jump to content
  • Advertisement
Sign in to follow this  
theSecondt

[C++] why the file streams don't work?

This topic is 4308 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've 1st put the ofstream-related code in main.cpp and it worked perfectly. now i've put it in a class and it fails miserably. my error: "106 \Dev-Cpp\lib\gcc\mingw32\3.4.2\include\stdarg.h `__gnuc_va_list' does not name a type " my small code: log.h
#ifndef LOG_H_
#define LOG_H_

#include <string>
#include <fstream>

class CLog
{
  public:
    CLog(){ main_log.open("main.log", std::ios::out | std::ios::app); if(!main_log.is_open()) printf("!!! CANNOT LOG TO FILE\n"); }
    ~CLog(){ main_log.close(); }

    void MAIN(const std::string LOG_STR);

  private:
    std::ofstream main_log;

}

#endif  //LOG_H_

log.cpp
#include "CLog.hpp"

void CLog::MAIN(const std::string LOG_STR)
{
  main_log << LOG_STR << std::endl;
}

main.cpp
int main ( int argc, char *argv[] )
{
  CLog LOG;

  LOG.MAIN("project started.");
.
.
.

Share this post


Link to post
Share on other sites
Advertisement
It's probably caused by a missing include. __gnuc_va_list is likely related to a variadic function, so printf looks suspicious.

You're using printf but not including <cstdio>. You're also explicitly using the std namespace.

I'd try:

#ifndef LOG_H_
#define LOG_H_

#include <string>
#include <fstream>
// include cstdio, which has the prototype for printf
#include <cstdio>

class CLog
{
public:
CLog(){
main_log.open("main.log", std::ios::out | std::ios::app);
// use the std namespace explicitly
if(!main_log.is_open()) std::printf("!!! CANNOT LOG TO FILE\n");
}
~CLog(){ main_log.close(); }

void MAIN(const std::string LOG_STR);

private:
std::ofstream main_log;
}

#endif //LOG_H_



To be honest, I see no reason for using printf instead of streams seeing how you're already using streams for logging.

Also, in "log.cpp", you are including "CLog.hpp", but there is no such header. Trying including "log.h" instead, or renaming "log.h" to "CLog.hpp".

Finally, a semicolon is also missing at the end of the declaration of CLog, and main.cpp should include "log.h" as well.


Hope this helps.

Share this post


Link to post
Share on other sites
i laughed at loud when i read:
Quote:

Finally, a semicolon is also missing at the end of the declaration of CLog, and main.cpp should include "log.h" as well.


hehe, i would never have noticed it, tyvm!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!