I've tried to read the thread below about creating global variables that can be used between multiple files but it will not work.
Global Variable within Multiple Files
variable.h
extern const int variable1;
file1.h
class fileOne{
private:
fileTwo ft[variable1];
public:
//some functions...
};
file2.h
class fileTwo{
private:
//some variables
public:
//some functions
};
main.cpp
int main(){
int variable1 = 2;
fileOne fo;
}
When I run this, the compiler says the following:
error: array bound is not an integer constant before ']' token
Is it possible to declare a global variable and use it in the manner above?
Also: Is this an array? fileTwo ft[variable1];
Is fileTwo a class?
A fixed-sized array needs to have its size specified at compile-time, which means only a compile-time constant can be used. You can't use an extern
ed variable for the size of the array, even if it is declared as const
, as the compiler simply doesn't know the actual value of the variable since it is in another translation unit.
Also, in main.cpp
, you can't declare the extern
'ed variable as a local variable, it needs to be in global scope instead.
For what you are trying to do, there is no reason to use extern
at all. Just declare variable1
with a constant value directly in variable.h
, and then #include
that file where needed, eg:
variable.h:
#ifndef VARIABLE_H
#define VARIABLE_H
static const int variable1 = 2;
#endif
file1.h:
#ifndef FILE1_H
#define FILE1_H
#include "file2.h"
#include "variable.h"
class fileOne{
private:
fileTwo ft[variable1];
public:
//some functions...
};
#endif
file2.h:
#ifndef FILE2_H
#define FILE2_H
class fileTwo{
private:
//some variables
public:
//some functions
};
#endif
main.cpp:
#include "file1.h"
int main(){
fileOne fo;
}
Is it possible to declare a global variable and use it in the manner above?
Yes, but like Remy pointed out, you would need to move this variable to a header file that will be #include
d from file1.h
Also: Is this an array?
fileTwo ft[variable1];
Yes, this is called a C-array. The C++ standard library also has C++ arrays (ie fixed size) using std::array<type, size>
, which are much more convenient and safe to use, as well as containers like vector
, list
, etc.
If you want to use a C++ array, you can declare it this way for you case:
#include <array>
std::array ft<fileTwo, variable1>;
To define an array you need to specify the actual size of the array and the size must be something the compiler can turn into a constant. Since variable1
can't be turned into a constant, the compiler issues the error.
What you can do is something like the following.
file1.h
class fileOne{
private:
fileTwo ft[variable1];
public:
//some functions...
};
file2.h
class fileTwo{
private:
//some variables
public:
//some functions
};
main.cpp
static const int variable1 = 2; // const int for the size of the ft array of class fileOne
#include "file2.h" // class fileTwo definition is needed for class fileOne
#include "file1.h" // class fileOne has a dependency on class fileTwo
int main(){
fileOne fo; // define a fileOne object named fo
// do more stuff
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.