Jump to content
  • Advertisement
Sign in to follow this  
frogtag

Declaring Extern Classes overandoverandover!

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

Basically, I have a window class... window.h
#ifndef WINDOW_H
#define WINDOW_H

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>


class Window
{
  private:
  public:
    bool OpenWindow(char*);
}

window.cpp
#include "window.h"

bool Window::OpenWindow(char* EngineName)
{
 ...
}

I declare the window class in my main.cpp, Window Window1; The problem is everytime I want to use the Window class in another file... eg, DrawWorld.cpp i have to add

#include "window.h"

extern Window Window1;

This all works fine. I just don't want to have to redeclare it each time I want to use it. I just want to add #include "window.h" and bang, ready to roll, nice and incoporated? Any ideas? I'm using C++/OpenGL on Dev-cpp [Edited by - frogtag on December 5, 2007 3:55:23 AM]

Share this post


Link to post
Share on other sites
Advertisement
The easiest way to do it is to declare the variable in your window.cpp and then extern it in window.h.

Share this post


Link to post
Share on other sites
so...

window.cpp
----------
Window Window1;

window.h
----------
extern Window Window1;

Then everytime i link to window.h, it sets up. One of my original ideas. Doesn't work though. Get a "Window does not name a type" message.

Share this post


Link to post
Share on other sites
That should work but without proper errors/code we cant help you...
Note that when you extern a variable it doesn't create one.
Just tells the compiler that this variable was declared somewhere.

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
so...

window.cpp
----------
Window Window1;

window.h
----------
extern Window Window1;

Then everytime i link to window.h, it sets up. One of my original ideas. Doesn't work though. Get a "Window does not name a type" message.


You have to declare the extern after the class definition, not before:

window.h
----------

class Window {
...
};

extern Window Window1;




Share this post


Link to post
Share on other sites
OK... to bring you upto speed...

I've externally declared my Class after the class definition as so:

window.h
----------

class Window {
...
};

extern Window Window1;

The declaration takes place in the source file, as so:

window.cpp
----------
#include "window.h"

Window Window1;

[functions()...etc...]

And the external source/header files, link to the class via to the header file as so:

genericfile.h
-------------
#include "window.h"

This looks like wants to work, but still brings up an error.

(in window.cpp) 'Window' does not name a type
[Build Error] [window.o] Error 1

Anyone tell me what i'm doing wrong?

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
OK... to bring you upto speed...

I've externally declared my Class after the class definition as so:

window.h
----------

class Window {
...
};

extern Window Window1;

The declaration takes place in the source file, as so:

window.cpp
----------
#include "window.h"

Window Window1;

[functions()...etc...]

And the external source/header files, link to the class via to the header file as so:

genericfile.h
-------------
#include "window.h"

This looks like wants to work, but still brings up an error.

(in window.cpp) 'Window' does not name a type
[Build Error] [window.o] Error 1

Anyone tell me what i'm doing wrong?


Are there any other errors before that point? Does it work when you try to compile (without linking) just window.cpp by itself? Are there any macros lurking about that could possibly change things in strange ways?

Share this post


Link to post
Share on other sites
Sign in to follow this  

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