Sign in to follow this  

C++ extern problem

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

This topic is 4381 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this