Archived

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

Poontardis

class instances as externs?

Recommended Posts

Poontardis    122
Is it possible to declare an instant of a class as extern or is it only possible with variables and functions? e.g. main.cpp
        
#include "main.h"

#include "myclass.h"


extern cmyclass *myinst = NULL;

void initfunction()
{
    myinst = new cmyclass();
    afunction();
}

//more code, etc...
.
.
.
[/source]

//please excuse the fact that the message board software can't handle two lots of source code in one message <img src="smile.gif" width=15 height=15 align=middle>


other.cpp:

[source]
#include "main.h"

#include "myclass.h"

#include "other.h"


cmyclass *myinst;

void afunction()
{
    myinst->dosomething();
}

//code, blah...
.
.
.
    
I keep getting compiler errors telling me that myinst is being redeclared. Is this a syntax type error on my part or is it not possilbe to do this with class instances? Help me Edited by - poontardis on 7/20/00 6:06:33 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Hye !

at least 2 source files call your header file "myclass.h" so your compiler wants to define your class again.

put an environment variable in your header file like this:

#ifndef ENV_VAR_MYCLASS
#define ENV_VAR_MYCLASS

.
.
//class declaration with its #includes, etc.
.
.

#endif

your compiler will define your class one time only for all your project

tell me if it works

Share this post


Link to post
Share on other sites
TomH    258
[Ask you might be able to guess this message was posted before seeing the previous post, but I thought I'd leave it anyway]

Poontardis, The answers your question is yes, you can use class instances as globals ( or declare them as extern ) So I guess it must be your syntax which is wrong, though I can't see any thing wrong with it.

Anyway here's a short example which uses an external class instance, though you'll have to bear with me which I try and figure out how best to insert source code into a message.

(Example compiled/ checked with djgpp)

---File globals.h---
#ifndef GLOBALS_H
#define GLOBALS_H

#include "foo.h"

extern CFoo* g_pFooInstance;

#endif

---File foo.h---
#ifndef FOO_H
#define FOO_H

class CFoo
{
private:
int m_i;
public:
CFoo ( int p_i );
~CFoo ( void ){};
};

#endif

---File foo.cpp---
#include

#include "foo.h"
#include "globals.h"

// Storage & initialisation for global
CFoo* g_pFooInstance = NULL;

// Constructor
CFoo::CFoo ( int p_i )
:m_i(p_i)
{};

---File main.cpp---
#include

#include "foo.h"
#include "globals.h"

int main ( void )
{
g_pFooInstance = new CFoo ( 2 );
delete g_pFooInstance;
g_pFooInstance = NULL;

return (0);
}

Edited by - TomH on July 20, 2000 8:45:03 AM

Share this post


Link to post
Share on other sites
Poontardis    122
I''ve got #defines to stop headers being compiled more than once, so that''s not the problem.

Having looked at your source code I can''t see what it is I''m doing wrong, so here''s a sort of overview of the context of my code:

I have a logfile class which is defined in my own files Error.h|cpp, an instant of this class is created by:

cError app_log = new cError("mylog.log", "Application Title", LOG_ERRORS);



this is implemented in my main unit (main.h|cpp) as:

#include "main.h"
#include "OpenGL_Util.h" //my own OpenGL unit
#include "error.h" //my own log file unit

extern cError app_log = NULL; //externally accessable class instance

void Init()
{
//initialise the application log file
app_log = new cError("app.log", "My App", LOG_ERRORS);
}

... blah

I also wish to use the same log file in other units (such as OpenGL_Util.h|cpp) when debugging where it is declared as:

#include "OpenGL_Util.h"

#ifdef _DEBUG
#include "error.h" //my own log file unit
#include "main.h" //main unit holding extern declaration of app_log

cError app_log;
#endif

void InitOpenGLFunc()
{
#ifdef _DEBUG
app_log->PrintLog("OpenGL initialisation beginning");
#endif

...blah
}

...





So, can anyone see what I''m doing wrong here? I get a linker error (sorry, I said compiler before) stating that app_log is being redeclared or something.


Cheers for your help so far, guys.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Ok a linker error !!

i already have it !

it''s possible you forgot to declare some libraries in your project

what''s your compiler ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
what do the error message exactly saying ?

Share this post


Link to post
Share on other sites
Poontardis    122
It''s ok guys, I was being thick as pigshit

I was declaring the instance as an extern in the main unit and just plain declaring it in all the other units I wanted to use it.

I should have declared as normal in the main unit and declared it as an extern in all the other units.

Duh

Cheers for your help though.

Share this post


Link to post
Share on other sites