Class pointer problem

Started by
6 comments, last by Mizipzor 18 years, 9 months ago
Im trying to create a log class which all my projects classes is going to write to. It looks like this:

#ifndef _LOG
#define _LOG

#include <fstream>

class Log {
private:
	std::ofstream log;		// the log file

public:
	Log()	{
		log.open("Log.txt");			//Automatically deletes any existing file, you have to tell it to append for it not to.
		std::clog.rdbuf(log.rdbuf());	//Redirect clog to output to file.
	}
	~Log()	{log.close();};

	// log file function
	void	Write(std::string message) {std::clog << message << std::endl;}
};

#endif



The "Game" class has an instance of the "AsciiMap" class. The AsciiMap wants an adress to a log class so it knows where to send the log strings. AsciiMap.h

#include "Log.h"

class AsciiMap {
private:
	Log*	Log;		// pointer to the log class

public:
			AsciiMap(int x, int y, Log* LogFile);	// sizes the ascii map to the size specified


The part in AsciiMap's constructor where I store the Log adress looks like this:
Log = LogFile;	// store the adress to the logfile, if NULL is sent, no logs will be written
Game has an instance of Log as well and when Game is created, it send the adress of Log to the AsciiMap class:
Game::Game() : AsciiMap( RESX / GRIDSIZE, RESY / GRIDSIZE, &Log) 
But it wont work and the compiler just spits errors at me. This is the first time Im using pointers for real and in the tutorials I only used them on a couple of int's and in small arrays. I think Ive missunderstood some key element here. Heres the errors I get:

AsciiMap.cpp
d:\Documents\CPP\Visual Studio Projects\SDL\AsciiMap.cpp(3) : error C2061: syntax error : identifier 'Log'
d:\Documents\CPP\Visual Studio Projects\SDL\AsciiMap.cpp(3) : error C2511: 'AsciiMap::AsciiMap(int,int)' : overloaded member function not found in 'AsciiMap'
        d:\Documents\CPP\Visual Studio Projects\SDL\AsciiMap.h(17) : see declaration of 'AsciiMap'



Advertisement
Log* Log

The name of the pointer is the same as the class it belongs to. That might be the problem. Try e.g.

Log* log_p
That was the problem I think, changed to cLog* Log. Many errors gone. But still, everywhere where I try to write to the log with:

*Log->Write(temp.str());

I get:

d:\Documents\CPP\Visual Studio Projects\SDL\Sdl_handle.cpp(15) : error C2100: illegal indirection
No, that will not work either.

<code>

Log* log_p; // this means: log_p is a pointer to an instance of Log

// non-static functions should be called on instances, not the class itself

log_p->Write(temp.str());

// this is the same as

*log_p.Write(temp.str());

</code>
Thanks for explaining that. :) Its all cleared out now.

Oh, and btw, its [.code] //... [./code] whitout dots. Thanks again. :)
OK, I was already trying to figure that out, thanks!
:-)

Bye
Instead of passing all those pointers around, why don't you make your member function Write() a static function. This way your other classes only need to include log.h in thier headers and call log::Write() to write the file.

There are some rules for static members of a class so be sure follow them. The main one is that a static function can only work on static data, so anything that the static function manipulates has to be static as well.

Here is my log class that I use in my projects. I stole it from somewhere, I think it was an article here on gamedev.net

#ifndef LOGGER_H#define LOGGER_H#include <cstdio>			// We Need The Standard IO Header#include <cstdlib>			// The Standard Library Header#include <cstdarg>			// And The Standard Argument Header For va_listusing namespace std;//Defines#define LOG_FILE	"NIGHT_log.txt"class logger{	public:		logger();		~logger();		bool init_log();		void close_log();				static void log(char* input, ...);			private:		static FILE *log_file;								// The File For Error Logging};#endif


and the implementation

#include "logger.h"//static log file.  Implementation here.FILE* logger::log_file;/************************************************************This is the function that initializes the system loggerInput: NoneOutputs: None************************************************************/logger::logger(){	init_log();}/************************************************************This is the function that shuts down the system loggerInput: NoneOutputs: None************************************************************/logger::~logger(){	close_log();}/************************************************************This is the function that initializes the system loggerInput: NoneOutputs: true/false if can open the file for logging************************************************************/bool logger::init_log(){	if(!(log_file = fopen(LOG_FILE, "w")))	{		perror("Can't init Logfile!\n" );		exit(1);	}	log(" -- Log Init...\n");	return true;}/************************************************************This is the function that closes the system loggerInput: NoneOutputs: None************************************************************/void logger::close_log(){	log("\n-- Closing Log...\n");	if(log_file)	{		fclose(log_file);	}}/************************************************************This is a static function that actually writes a line in the log.It flushes the log after every entry so that in a crash, the log file will have the info it needs.Input: line to logOutputs: writes a line to the log file************************************************************/void logger::log(char* input, ...){	va_list arg;	va_start(arg, input);	if(log_file)	{		vfprintf(log_file, input, arg);		fprintf(log_file,"\n");		//be sure to flush the file so that the info gets to disk		fflush(log_file);	}	va_end(arg);}

[size="3"]Halfway down the trail to Hell...
That would definately be better... Ill toy around a bit and see if I can make something good out of it. Looking up some tutorials on the subject to.

This topic is closed to new replies.

Advertisement