Skip to main content
GameDev.net gamedev.net
🔒 Locked

Static Function Pointer Member

Started by Mihkael Aug 27, 2000 at 12:21 AM 2 replies 800+ views
Original Post
Mihkael
Mihkael
Okay, anyone that can help me with my following problem? I am writing a class to handle all of my errors. One thing I want though, is for this class to be completely platform independent. So, I don't want to be doing any MessageBoxes or PostQuitMessages in it. However, I want the programmer to be able to set the function of how they will handle errors, which, when an error gets sent to the errorhandler class, it will then pass it along to their function... Here is some of the code:
        
//ERRORMESSENGER.H

#ifndef ERRORMESSENGER_H
#define ERRORMESSENGER_H

typedef void(*FunctionPtr)(char*, bool);


class ErrorMessenger  
{
public:
    static void SetHandler(FunctionPtr Handler);
    ErrorMessenger();
    ~ErrorMessenger();

private:
    static FunctionPtr m_ErrorFunction;

};

void HolderFunction(char *szTemp, char *bTemp);
#endif


//

//ERRORMESSENGER.CPP

#include "ErrorMessenger.h"

//Static Member Initialization - Also spot of error it seems

FunctionPtr m_ErrorFunction = Holder;

ErrorMessenger::ErrorMessenger()
{//Empty

}

ErrorMessenger::~ErrorMessenger()
{//Empty

}

void ErrorMessenger::SetHandler(FunctionPtr Handler)
{
 m_ErrorFunction = Handler;
}

void Holder(char * szTemp, bool bTemp)
{

}
    
anyways, when I compile this, I get the following Error Message: ErrorMessenger.obj : error LNK2001: unresolved external symbol "private: static void (__cdecl* ErrorMessenger::m_ErrorFunction)(char *,bool)" (?m_ErrorFunction@ErrorMessenger@@0P6AXPAD_N@ZA) Pointing to where I initialize m_ErrorFunction. Any ideas as to what I am doing wrong?? I would greatly appreciate your help!! Thank you, Mihkael Edited by - Mihkael on 8/27/00 12:23:27 AM
baskuenen
baskuenen
quote:
Original post by Anonymous Poster

Iäm not sure, but I think you must "define" the static member-variable outside of your class (in addition to the declaration you already have).
So try adding the following line outside of the class and see how it goes.

        
static FunctionPtr ErrorMessenger::m_ErrorFunction = NULL; // Or some other value.



Hope it helps...



I second that AP



Dormeur
Dormeur
Why not use a more Object oriented approach to solve this problem? Instead of using the "C-like" solution of using static function pointers, you could use the ErrorMessenger class as an event handler, that you register with the "main class" (or whatever) at startup. The ErrorMessenger then has a virtual member that gets called on error. The programmer only has to derive the ErrorMessenger class and override the error handling method, and register an instance of the class to make his own error handler. Something like this:

    
#include <iostream>

using namespace std;

class ErrorMessenger
{
public:
virtual void DoError(const char *msg) {
// Do nothing

}
};

// My own error handler

class MyErrorMessenger
{
public:
virtual void DoError(const char *msg) {
cout << "Error: " << msg << endl;
}
};

// The "main" class

class MainClass
{
// The registered error handler

ErrorMessenger *errorMessenger;
public:
MainClass() {
// Initialize with default error messenger

errorMessenger = new ErrorMessenger();
}
// Error stuff

void ErrorOccurred(const char *msg) {
errorMessenger->DoError(msg);
}
void SetErrorHandler(ErrorMessenger *newHandler) {
delete errorMessenger;
errorMessenger = newHandler;
}
};

int main()
{
MainClass mainClass();
// Register own version of the error handler

mainClass.SetErrorHandler(new MyErrorMessenger());
// ...

return 0;
}


It''s something that''s used widely in Java, and I think it''s pretty neat, and very flexible!

Dormeur

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.