Posts: 6,419
Threads: 74
Joined: Mar 2002
Ah. I thought you meant my code didn't work. Next time quote
I'll take a galnce at yer code.
Posts: 691
Threads: 5
Joined: Apr 2002
You only need to use extern in files where the variable is external. Try this simple example:
number.h
Code:
/* Number.h */
void increment(void);
typedef struct {
int a;
} number;
main.c
Code:
/* main.c */
#include <stdio.h>
#include "number.h"
number testNumber;
int main() {
testNumber.a = 0;
printf("Test number = %d\n", testNumber.a);
increment();
printf("Test number = %d\n", testNumber.a);
}
increment.c
Code:
/* Increment.c */
#include "number.h"
extern number testNumber;
void increment(void) {
testNumber.a++;
}
Compile both increment.c and main.c together to produce an executable. The structure number is defined in main.c and is an external structure in increment.c
esus saves.... Passes to Moses, shoots, he scores!
Posts: 222
Threads: 23
Joined: May 2003
I just don't understand. What's wrong here?
MAIN.CPP
Code:
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include "messages.cpp"
#include "display.cpp"
typedef struct
{
char name;
} number1;
number1 character;
int main()
{
setup_video();
setup_game_data();
splash_screen();
return 0;
}
DISPLAY.CPP
Code:
void setup_video();
void setup_video()
{
textmode(C80);
clrscr();
textcolor(LIGHTGRAY);
}
MESSAGES.CPP
Code:
void setup_game_data();
void splash_screen();
char ask(char question);
extern number1 character;
void setup_game_data()
{
character.name = ask("What is your name?");
}
void splash_screen()
{
cout << "Message Here.";
}
char ask(char question)
{
cout << question;
cin << ask;
return ask;
}
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Posts: 691
Threads: 5
Joined: Apr 2002
You will get an error with your code that character has an unknown storage class in messages.cpp. From your code for messages.cpp:
Code:
void setup_game_data();
void splash_screen();
char ask(char question);
extern number1 character;
void setup_game_data()
{
character.name = ask("What is your name?");
}
void splash_screen()
{
cout << "Message Here.";
}
char ask(char question)
{
cout << question;
cin << ask;
return ask;
}
What exactly is the type number1? You can't carry the definition over from main.cpp, you either need to move the typedef for number1 into a common header file, like I did in the example I gave you, or have the typedef in both main.cpp and messages.cpp.
esus saves.... Passes to Moses, shoots, he scores!
Posts: 222
Threads: 23
Joined: May 2003
Ah... of course. Silly me
.
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Posts: 1,845
Threads: 44
Joined: Aug 2002
Quote:
I should change this to:
Posts: 222
Threads: 23
Joined: May 2003
Yeah... I noticed that (and managed to fix something for once.) I've still got compiler errors up the billy-oh, but I'll get over it.
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"