Jump to content
  • Advertisement
Sign in to follow this  
Photonman

C++ extern problem

This topic is 4593 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 a program that needs to use a struct object declared in another file. Basically, the main file includes a header file, and the struct is declared and defined in the implementation file of the header (which is not explicitly included in the main file): implementation file: typedef struct vehicle { int x,y; int dir,speed; int color; int score; } car[2]; In my main file, I had the following: extern vehicle car[2]; And when I tried to compile it, I got the following error: "'vehicle' does not name a type" I have no clue why this is happening....any ideas? Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement

Use

struct vehicle {
int x,y;
int dir,speed;
int color;
int score;
};

and

extern vehicle car[2];

Share this post


Link to post
Share on other sites
Well right now, you are already declaring the struct, then trying to redeclare it again and you have the places of where it goes mixed up.

// In a header file that is incuded by anything that needs to use the vehicle class.
// vehicle.h
struct vehicle
{
int x,y;
int dir,speed;
int color;
int score;
};

extern vehicle car[2];

// Implementation file for main or w/e
#include "vehicle.h"

// This just needs to happen once, so it needs to be in a .cpp file
vehicle car[2];

Share this post


Link to post
Share on other sites
Well - two things. First, if you're going to put extern vehicle car[2]; vehicle needs to be a type which has been defined in that compilation unit, i.e. you need to put the definition in the header file or something equivalent. Second, the code you posted defines car as a type of array containing two vehicles, because of the typedef, not as an actual array of two vehicles. typedef in front of struct definitions is only usually used in C not C++. If you're using C then, you want a header file like:


typedef struct vehicle_ {
int x,y;
int dir,speed;
int color;
int score;
} vehicle;

extern vehicle car[2];


And vechicle car[2] in the implementation file.

If it's C++, what nmi said.

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!