So in my namespace's .h file, I have
namespace wtvr{
static Matrix m;
void LoadIdentity(void);
};
and in its .cpp file, I have
namespace wtvr{
void LoadIdentity(void){
m = Identity();
m.display();// trace for debugging
}
};
else where in the main program
wtvr::LoadIdentity();
wtvr::m.display();
The first display() prints the identity matrix to the screen from within the LoadIdentity() function, but the second, which is after the function returns, displays all zeros. Why are my values disappearing? Is there a different way I should be making my global? Thanks
You've declared static Matrix m;
in the header file. This means that each .cpp file that includes that header will get its own version of m
.
instead you need to make it a global (although namespace-scoped) variable.
In the header file:
namespace wtvr{
extern Matrix m;
};
In any of the .cpp files:
namespace wtvr{
Matrix m;
};
You've declared the variable as static
, which means each translation unit (.cpp file) has its own copy of it. You probably meant this:
.h file:
namespace wtvr{
extern Matrix m; //declare for use from everywhere
void LoadIdentity(void);
}
.cpp file:
namespace wtvr{
Matrix m; //define in exactly one .cpp file
void LoadIdentity(void)
{
m = Identity();
m.display();// trace for debugging
}
}
Yes, there is a different way you should be making your global.
In the .h file:
extern Matrix m;
In its .cpp file:
Matrix m;
void LoadIdentity(void)
{
m = Identity();
m.display();
}
No other file needs to change. static
in file scope means that each file gets its own copy of the variable that isn't linked with other files' copies of the variable, which is the opposite of what you want.
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.