C++ extern problem
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
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.hstruct 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 filevehicle car[2];
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:
And vechicle car[2] in the implementation file.
If it's C++, what nmi said.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement