Sign in to follow this  

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

This topic is 4096 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
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

This topic is 4096 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this