Jump to content
  • Advertisement

Archived

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

Mihkael

Static Function Pointer Member

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

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

Share this post


Link to post
Share on other sites
Advertisement
Guest 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...

Share this post


Link to post
Share on other sites
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



Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • 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!