473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Can't access class members from function

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
Jul 19 '05 #1
2 3406
"Stuart P" <st*****@gvec.removethis.net> wrote...
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;
This is really not what you want to post here. MS-specific type
nonsense should better be limited to MS-specific newsgroups.
Besides, if you use real C++ types, you're probably going to see
much clearer into what you're trying to do.

So, let's say you have

unsigned char w, h;
unsigned short *boarddata;
char *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[]="";
How should this one-char array help you?
BYTE temp2;
Again, let's use normal C++ types:

unsigned char temp2;
std::ifstream lvlfile(fname, std::ios::in | std::ios::binary);

// irrelevant stuff went here

temp2=(BYTE)lvlfile.get();
'get' returns 'int_type'. Converting it to unsigned char is,
most likely, not what you want. So, you may be better off with
'temp2' declared as 'int'...
tsetfile=new char[temp2+1]; // crash occurs here
There is nothing on that line to suggest the reason for it to
crash, _unless_ 'temp2' has the value < -1. Have you tried to
check what value your 'temp2' has 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.
No. If you can't access the member variables, the compiler would
complain. If it compiles OK, access is not the cause.

I think I might need to initialize something somehow first but I'm not sure how...


It is possible that the error is somewhere in "irrelevant stuff"
as you labelled it.

Victor
Jul 19 '05 #2
>
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

}

Since tsetfile is of type LPTSTR, do
tsetfile = new TCHAR[temp2+1];
tsetfile[temp2] = _T('\0'); // terminate the string
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
4
by: Eric A. Johnson | last post by:
Hi All, I have a class, ConsoleWindow, that is a member of another class, ConsoleLib, like so: class ConsoleLib { public: class ConsoleWindow { public:
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
5
by: zqhpnp | last post by:
class String { public: String& operator=(const String& str); private: char* pdata; } String& String::operator=(const String& str) { if(this==&str)
8
by: Tapeesh | last post by:
I have a following piece of code. The code was compiled using g++ class A { public : virtual void fn() = 0; }; class B: virtual private A {
13
by: dragoncoder | last post by:
Consider the following code #include <iostream> class Base { public: virtual void say() { std::cout << "Base" << std::endl; } }; class Derived: public base {
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.