Sign in to follow this  
furnace

C++ includes/externs - Help?!

Recommended Posts

Hi all, I'm having some trouble. My code is nice and short so please have a look! The problem I'm having is with externs/includes. Usually this works fine with, for example, a char or integer etc. But now I am doing it with an array of booleans and it isn't working! I am using Visual C++ 2003. The error I get is within functions.cpp, it highlights "keys[5]=true;" and says "error C2109: subscript requires array or pointer type". I understand this means my keys variable isn't an array. What I don't understand is *why* it isn't an array! The error is a compile error, not a runtime error. Here is the code: main.cpp
#include <windows.h>		// Header File For Windows
#include <stdio.h>
#include "globals.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	keys[5]=true;
	MessageBox(NULL,"Goodbye","Message",MB_OK);
	return 0;
}
globals.h
bool keys[256];
functions.h
int myFunction(void);
functions.cpp
#include <windows.h>		// Header File For Windows
#include <stdio.h>
extern keys;

int myFunction(void){
	keys[5]=true;    // <---- ERROR HERE
	MessageBox(NULL,"Text","Test",MB_OK);
	return 0;
}
Thank you so much for your help :)

Share this post


Link to post
Share on other sites
#include <windows.h>		// Header File For Windows
#include <stdio.h>
extern bool keys[256];

int myFunction(void){
keys[5]=true; // <---- ERROR HERE
MessageBox(NULL,"Text","Test",MB_OK);
return 0;
}
You need to tell the compiler the type of your externed variable.

Σnigma

Share this post


Link to post
Share on other sites
I tried specifying the type of the variable but didn't try specifying the array size. Works fine now! :) Thanks.

I might start a new thread on this so I can have/start a proper discussion on the topic, but everywhere I read they say "global variables are bad" and that things should be in classes.

But even if it were in a class, it would have to be a globally-defined class wouldn't it? Or would you just pass a reference/pointer to the class in between functions so that the functions that need it never look in the global scope? (If that makes sense)

Share this post


Link to post
Share on other sites
Global variables are annoying when used in large projects. For small projects, or throwaway prototypes, they are not as bad.

The Game has an object of the IInput inteface which provides it with high-level input in terms of actions and not keys (that is, the game needs to see if a "FIRE_WEAPON" action is required, not if the "w" key is pressed).

One of the classes which implement IInput is LiveInput, which reads key data from the keyboard and uses a configurable binding system to transform key presses into actions. The LiveInput class is the only class which needs to be passed the keyboard state, because nobody else needs it.

Share this post


Link to post
Share on other sites

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