Jump to content
  • Advertisement
Sign in to follow this  
Zdman

Mulitple instance of variable issue

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

I have 3 files for my project: ogl.h, ogl.cpp, main.cpp(all mine) When i compile(using vc++ or dev-c++) i get that these variables have mulitple instances. int ogl_iWidth; int ogl_iHeight; int ogl_iBits; bool ogl_bFullscreen; They are global variables in ogl.h and used in ogl.h, but for some reason there is an issue. There are NO OTHER VARIABLES DECLARED AS THESE ARE!!! So, there is some other problem any ideas?

Share this post


Link to post
Share on other sites
Advertisement
I'm guessing you're including ogl.h in both ogl.cpp and main.cpp? If so, learn how to use #ifndef to make sure header files are only included once. Here's a link explaining this better.

Share this post


Link to post
Share on other sites
Better yet, don't put global variables in header files. Define them in a single source file and declare them as extern in the header file.

Share this post


Link to post
Share on other sites
I have used #ifndef

#ifndef OGL_H
#define OGL_H

//#pragma warning(disable:4305)
//#pragma comment(lib, "lglu32.lib")
//#pragma comment(lib, "lopengl32.lib")

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>

const float PI = 3.1415926535897932384626433832795;

int ogl_iWidth;
int ogl_iHeight;
int ogl_iBits;
bool ogl_bFullscreen;

//OGL
HDC ogl_hDC;
HGLRC ogl_hRC;

//OGL Setup
bool ogl_SetMode(int tempWidth, int tempHeight, int tempBits, bool tempFullscreen);
void ogl_Init(HWND hWnd);
void ogl_UnInit();
void ogl_SetupPixelFormat();
void ogl_Resize(int Width, int Height);
void ogl_SetOrtho();
bool ogl_LoadTexture(char *TexName, GLuint TexHandle)
//OGL Draw
void ogl_StartDraw();
void ogl_Swap();
void ogl_ClearScreen();
void ogl_SetColor(float R, float G, float B);
void ogl_SetPointSize(float Size);
void ogl_DrawPoint(float x, float y, float z);
void ogl_DrawPoint(GLfloat v[]);
void ogl_SetLineWidth(float Size);
void ogl_DrawLine(GLfloat v1[], GLfloat v2[]);
void ogl_DrawStippleLine(GLfloat v1[], GLfloat v2[], short stipplePattern, int factor);
void ogl_DrawTriangle(GLfloat v1[], GLfloat v2[], GLfloat v3[]);
void ogl_DrawTriangleStrip(GLfloat v1[][3], int vertices);
void ogl_DrawTriangleFan(GLfloat v1[][3], int vertices);
void ogl_DrawSquare(GLfloat v1[], GLfloat v2[], GLfloat v3[], GLfloat v4[]);
void ogl_DrawPolygon(GLfloat v1[][3], int vertices);
void ogl_SetModeFill();
void ogl_SetModeLine();
void ogl_SetModePoint();
void ogl_DrawCircle(float radius, int numberOfSides);

#endif

Share this post


Link to post
Share on other sites
Sweet, I just moved those globals to ogl.cpp and it works fine. Guess you can't have globals in a .h file.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zdman
I have used #ifndef

#ifndef OGL_H
#define OGL_H

...snip...

#endif


That only guards against multiple includes per translation unit. The previous advice is the correct solution.

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!