Jump to content
  • Advertisement
Sign in to follow this  
Marty666

problem with extern

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

Hi all, I use a 'stdafx.h' for all my constants, globals, etc. I have all the globals in their own cpp files, but in 'stdafx.h' they are defined as 'extern'. This works fine, but when i try to use my own types i get these errors:
c:\...\stdafx.h(22) : error C2146: syntax error : missing ';' before identifier 'INSECTOR'
c:\...\stdafx.h(22) : fatal error C1004: unexpected end of file found
INSECTOR is defined in 'universe.cpp' as: TVec3ub INSECTOR = TVec3ub(0,0,0); and TVec3ub is a type i defined in '3dmath.h' I tried including '3dmath.h' in my 'stdafx.h', but that doesn't work... should i predefine TVec3ub in 'stdafx.h'? If so, how? Thanx, Marty

Share this post


Link to post
Share on other sites
Advertisement
From looking at those errors you may have an error in one of you header files, particulary that error about unexpected end of file found which is caused by a missing closing brace somewhere. If you do have an error in one of the header files this will have a knock on effect and cause other errors which might obscure the true source of the problem.

Share this post


Link to post
Share on other sites
this is my 'stfafx file'... when i leave out 'extern TVec3ub INSECTOR;' it all works fine. So I think the compiler just gives this error because it doesn't really know what's happening. I mean it doesn't know that it doesn't know TVec3ub. (wow, cool scentence). So what I think I need to do is tell the compiler what a TVec3ub is before it processes this bit. I tried by including '3dmath.h'... That doesn't work and would be a bad solution, because (not in my case, but it might happen) '3dmath.h' might include 'stdafx.h' and i get big trouble...
Is there a way for the compiler not to care at this point what TVec3ub looks like? Or is this a completely different problem from what i think it is?

Thanx,
Marty

Here's 'stdafx.h':


#pragma once

// constants
const char GAME_MODE_STRING[] = "800x600:32@60";
const float PI = (float)3.141592;

// globals
// IS.cpp
extern bool GAME_MODE;
extern int WindWidth;
extern int WindHeight;
extern double LastFrameTime;
extern double ThisFrameTime;
extern float FrameTime;
// textures.cpp
extern GLuint STARTEX;
// universe.cpp
extern int SECTORSIZE;
extern int MINSTARSINSECTOR;
extern int MAXSTARSINSECTOR;
extern unsigned char UNISIZE;
extern TVec3ub INSECTOR;

#define STATE_GOD 0;

Share this post


Link to post
Share on other sites
This may or may not be your problem, but I'll point it out anyway. In that code you have:
#define STATE_GOD 0;
With that if you have code it will copy the whole '0;' over, #define statements generally dont have semicolons after it.

Hopefully that fixes your problem
~Zix~

Share this post


Link to post
Share on other sites
The way I would solve it is by including 3dmath.h

What you want to do is have safety-statements (I know they're called something else, so sorry for the term) in your header files.

3dmath.h

#ifndef __MATH_H__ //means if-not-defined
#define __MATH_H__ //then we define

//all the 3dmath.h stuff here

#endif




stdafx.h

#ifndef __STDAFX__
#define __STDAFX__

#include "3dmath.h" //because of the statements in 3dmath.h, if it's already included it wont include again

//all those externs

#endif




Hopefully I can help more (Now that I was awake enough to read your posts]
~zix~

Share this post


Link to post
Share on other sites
Quote:
Original post by zix99
What you want to do is have safety-statements (I know they're called something else, so sorry for the term) in your header files.


The term you looking for is "include guards".

Regarding the original problem then including "3dmath.h" should have solved the problem but you mentioned that it doesn't. Could you post the code of 3dmath.h? There may be a problem with the way your declaring TVec3ub.

Share this post


Link to post
Share on other sites
Thanx,

this is 3dmath.h... all implementations are in the .h file aswell, i know i should make it a different file... but i don't think that's the problem, since it's defined here...

Marty


#pragma once

#include <math.h>
#include <memory.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "stdafx.h"

typedef struct TVec3
{
TVec3()
{X = 0; Y = 0; Z = 0;}
TVec3(float x, float y, float z)
{X = x; Y = y; Z = z ;}
TVec3 operator + (TVec3 AddVector)
{return TVec3(AddVector.X+X, AddVector.Y+Y, AddVector.Z+Z);}
TVec3 operator - (TVec3 SubVector)
{return TVec3(X-SubVector.X, Y-SubVector.Y, Z-SubVector.Z);}
TVec3 operator * (float Scalar)
{return TVec3(X*Scalar, Y*Scalar, Z*Scalar);}
TVec3 operator * (TVec3 DotVector)
{return TVec3(X*DotVector.X, Y*DotVector.Y, Z*DotVector.Z);}
TVec3 operator % (TVec3 CrossVector)
{return TVec3((Y*CrossVector.Z) - (Z*CrossVector.Y),(Z*CrossVector.X) - (X*CrossVector.Z),(X*CrossVector.Y) - (Y*CrossVector.X));}
TVec3 operator / (float Scalar)
{return TVec3(X/Scalar, Y/Scalar, Z/Scalar);}
float Size()
{return sqrt(X*X+Y*Y+Z*Z);}
void Normalize()
{float s = sqrt(X*X+Y*Y+Z*Z); X = X/s; Y = Y/s; Z = Z/s;}

bool operator == (TVec3 Compare)
{
if ((Compare.X == X) && (Compare.Y == Y) && (Compare.Z == Z)) {return true;}
else {return false;}
}
float X, Y, Z;
}TVec3;


typedef struct TVec3ub
{
TVec3ub()
{X = 0; Y = 0; Z = 0;}
TVec3ub(float x, float y, float z)
{X = x; Y = y; Z = z ;}
TVec3ub operator + (TVec3ub AddVector)
{return TVec3ub(AddVector.X+X, AddVector.Y+Y, AddVector.Z+Z);}
TVec3ub operator - (TVec3ub SubVector)
{return TVec3ub(X-SubVector.X, Y-SubVector.Y, Z-SubVector.Z);}
TVec3ub operator * (float Scalar)
{return TVec3ub(X*Scalar, Y*Scalar, Z*Scalar);}
TVec3ub operator * (TVec3ub DotVector)
{return TVec3ub(X*DotVector.X, Y*DotVector.Y, Z*DotVector.Z);}
TVec3ub operator / (float Scalar)
{return TVec3ub(X/Scalar, Y/Scalar, Z/Scalar);}
bool operator == (TVec3ub Compare)
{
if ((Compare.X == X) && (Compare.Y == Y) && (Compare.Z == Z)) {return true;}
else {return false;}
}
unsigned char X, Y, Z;
}TVec3ub;


typedef struct TVec2
{
TVec2()
{X = 0; Y = 0;}
TVec2(float x, float y)
{X = x; Y = y;}
TVec2 operator + (TVec2 AddVector)
{return TVec2(AddVector.X+X, AddVector.Y+Y);}
TVec2 operator - (TVec2 SubVector)
{return TVec2(X-SubVector.X, Y-SubVector.Y);}
TVec2 operator * (float Scalar)
{return TVec2(X*Scalar, Y*Scalar);}
TVec2 operator * (TVec2 DotVector)
{return TVec2(X*DotVector.X, Y*DotVector.Y);}
TVec2 operator / (float Scalar)
{return TVec2(X/Scalar, Y/Scalar);}
float Size()
{return sqrt(X*X+Y*Y);}
void Normalize()
{float s = sqrt(X*X+Y*Y); X = X/s; Y = Y/s;}
bool operator == (TVec2 Compare)
{
if ((Compare.X == X) && (Compare.Y == Y)) {return true;}
else {return false;}
}
float X, Y;
}TVec2;



typedef struct TMat4
{
TMat4() // Empty matrix
{
for (int i = 0; i < 16; i++)
{
if (i%5) {M = 0;}
else {M = 1;}
};
}
void SetValues(float m[16])
{memcpy(this->M, m, 16*sizeof(float));}
void Transpose() //////////
{;}
TMat4 operator + (TMat4 AddMatrix)
{
TMat4 temp;
for(int i = 0; i < 16; i++) {temp.M = M + AddMatrix.M;}
return temp;
}
TMat4 operator - (TMat4 SubMatrix)
{
TMat4 temp;
for(int i = 0; i < 16; i++) {temp.M = M - SubMatrix.M;}
return temp;
}
TMat4 operator * (TMat4 MultMatrix)
{
TMat4 temp;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; i++)
{
temp.M[i*4+j] =
M[i*4+0] * MultMatrix.M[0+j]
+ M[i*4+1] * MultMatrix.M[4+j]
+ M[i*4+2] * MultMatrix.M[8+j]
+ M[i*4+3] * MultMatrix.M[12+j];
}
}
return temp;
}
float Determinant()
{;}
// void GlMultMatrix()
// {glMultMatrixf(M);}
float M[16];
}TMat4;



typedef struct TQuat
{
public:
TQuat()
{X=0; Y=0; Z=0; W=1;}
TQuat(float x, float y, float z, float w)
{X = x; Y = y; Z = z; W = w;}
TQuat operator + (TQuat AddQuat)
{return (TQuat(X + AddQuat.X, Y + AddQuat.Y, Z + AddQuat.Z, W + AddQuat.W));}
TQuat operator - (TQuat SubQuat)
{return (TQuat(X - SubQuat.X, Y - SubQuat.Y, Z - SubQuat.Z, W - SubQuat.W));}
TQuat operator * (float scalar)
{return (TQuat(X * scalar, Y * scalar, Z * scalar, W * scalar));}
TQuat operator * (TQuat q)
{TQuat temp;
temp.W = W * q.W - X * q.X - Y * q.Y - Z * q.Z;
temp.X = W * q.X + X * q.W + Y * q.Z - Z * q.Y;
temp.Y = W * q.Y + Y * q.W + Z * q.X - X * q.Z;
temp.Z = W * q.Z + Z * q.W + X * q.Y - Y * q.X;
temp.Normalize(); return temp;}
TQuat operator * (TVec3 v)
{TQuat V (v.X, v.Y, v.Z, 0); TQuat temp;
temp.W = - X * V.X - Y * V.Y - Z * V.Z;
temp.X = W * V.X + Y * V.Z - Z * V.Y;
temp.Y = W * V.Y + Z * V.X - X * V.Z;
temp.Z = W * V.Z + X * V.Y - Y * V.X;
return temp;}
TQuat operator / (float scalar)
{TQuat temp;
temp.X = X / scalar; temp.Y = Y / scalar; temp.Z = Z / scalar; temp.W = W / scalar;
return temp;}
float Length()
{return (float)(sqrt(X * X + Y * Y + Z * Z + W * W));}
void Normalize()
{*this = *this / this->Length();}
TQuat Conjugate()
{return TQuat(-X, -Y, -Z, W);}
void RotateEuler(float xrot, float yrot, float zrot)
{TQuat temp1, temp2;
temp1.SetEuler(xrot, yrot, zrot);
temp2 = *this;
*this = (temp2 * temp1);}
void SetEuler(float xrot, float yrot, float zrot)
{TQuat *temp = this; TQuat qX, qY, qZ;
qX = (TQuat ((float)sin(xrot/2), 0, 0, (float)cos(xrot/2)));
qY = (TQuat (0, (float)sin(yrot/2), 0, (float)cos(yrot/2)));
qZ = (TQuat (0, 0, (float)sin(zrot/2), (float)cos(zrot/2)));
qX.Normalize(); qY.Normalize(); qZ.Normalize();
*temp = (qX * qY) * qZ;
temp->Normalize();}
void RotateVector(TVec3 &v)
{TQuat temp; temp = ((*this * v) * this->Conjugate());
v.X = temp.X;
v.Y = temp.Y;
v.Z = temp.Z;}
TMat4 ExtractMatrix()
{
TMat4 m;
float x2, y2, z2, w2, xy, xz, yz, wx, wy, wz;
x2 = X * X; y2 = Y * Y; z2 = Z * Z; w2 = W * W; xy = X * Y;
xz = X * Z; yz = Y * Z; wx = W * X; wy = W * Y; wz = W * Z;
m.M[0] = float(1 - 2*(y2 + z2));
m.M[1] = float(2*(xy + wz));
m.M[2] = float(2*(xz - wy));
m.M[3] = 0;
m.M[4] = float(2*(xy - wz));
m.M[5] = float(1 - 2*(x2 + z2));
m.M[6] = float(2*(yz + wx));
m.M[7] = 0;
m.M[8] = float(2*(xz + wy));
m.M[9] = float(2*(yz - wx));
m.M[10] = float(1 - 2*(x2 + y2));
m.M[11] = 0; m.M[12] = 0; m.M[13] = 0; m.M[15] = 1;
return m;
}
void RotateGL()
{
float angle = (acos(W) * (360/PI));
glRotatef(angle, X, Y, Z);
}
float X, Y, Z, W;
}TQuat;



Share this post


Link to post
Share on other sites
I think this might be your problem:

typedef struct TVec3
{
//removed
}TVec3;


What your doing is typdef'ing TVec3 to be an alias for the structure, but then your also declaring a variable of the struct of the same name. Since your using C++ the typedef is redundant, this is only required in C to actually define a new type. What you can do is either this:


//typical C definition
typedef struct
{
//code
}TVec3;


or method below which is what you'll see used most of the time in C++:

struct TVec3
{
//code
};


The struct definition you have seems to be a hybrid of both which I suspect is the likely cause of your errors. Try adjusting your struct definitions to one of the above and recompiling.

Share this post


Link to post
Share on other sites
Ok, I tried replacing all the 'typedef stucts' with 'struct' (as in spudder's example), but it doesn't seem to change anything...
I allways used to do it this way and normally it works fine... only since i'm using the globals this way (in 'stfafx.h' with extern). It gives me errors when i'm using my own types.

Marty

PS: I defined a GLuint aswell but the compiler doesn't give me any problems with that. I'll try to find out what is different in the gl.h file's declarations. I think it's just because it's an int and not a struct.

[Edited by - Marty666 on August 6, 2005 11:15:46 AM]

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!