S
Stuart P
Hello,
I am making my first real game in C++ and there is a problem with the class
used to contain a level. The level::load(filename) function gives an Access
Violation error when trying to access the class's member variables. Here is
the class definition in level.h:
class level {
BYTE w,h;
WORD *boarddata;
LPTSTR tsetfile;
public:
void load(LPCTSTR file);
void draw(IDirectDrawSurface7** target, IDirectDrawSurface7** tset);
};
and here is the function from level.cpp:
level::load(LPCTSTR fname) {
char temp[]="";
BYTE temp2;
std::ifstream lvlfile(fname, std::ios::in | std::ios::binary);
// irrelevant stuff went here
temp2=(BYTE)lvlfile.get();
tsetfile=new char[temp2+1]; // crash occurs here
lvlfile.get(tsetfile,temp2+1);
w=(BYTE)lvlfile.get(); // or here if above is commented
h=(BYTE)lvlfile.get(); // or here if above is commented
boarddata=new WORD[w*h];
// irrelevant stuff went here
}
I have tried using new char[10] and such but I am pretty sure the crash is
caused because it can't access the member variables.
I think I might need to initialize something somehow first but I'm not sure
how...
Any help would be greatly appreciated!
- Stuart
I am making my first real game in C++ and there is a problem with the class
used to contain a level. The level::load(filename) function gives an Access
Violation error when trying to access the class's member variables. Here is
the class definition in level.h:
class level {
BYTE w,h;
WORD *boarddata;
LPTSTR tsetfile;
public:
void load(LPCTSTR file);
void draw(IDirectDrawSurface7** target, IDirectDrawSurface7** tset);
};
and here is the function from level.cpp:
level::load(LPCTSTR fname) {
char temp[]="";
BYTE temp2;
std::ifstream lvlfile(fname, std::ios::in | std::ios::binary);
// irrelevant stuff went here
temp2=(BYTE)lvlfile.get();
tsetfile=new char[temp2+1]; // crash occurs here
lvlfile.get(tsetfile,temp2+1);
w=(BYTE)lvlfile.get(); // or here if above is commented
h=(BYTE)lvlfile.get(); // or here if above is commented
boarddata=new WORD[w*h];
// irrelevant stuff went here
}
I have tried using new char[10] and such but I am pretty sure the crash is
caused because it can't access the member variables.
I think I might need to initialize something somehow first but I'm not sure
how...
Any help would be greatly appreciated!
- Stuart