#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
class instances as externs?
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
July 20, 2000 07:08 AM
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
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
[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
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
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.
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.
July 20, 2000 08:41 AM
Ok a linker error !!
i already have it !
it''s possible you forgot to declare some libraries in your project
what''s your compiler ?
i already have it !
it''s possible you forgot to declare some libraries in your project
what''s your compiler ?
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement