Can't access class members from function

Discussion in 'C++' started by Stuart P, Jul 1, 2003.

  1. Stuart P

    Stuart P Guest

    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
     
    Stuart P, Jul 1, 2003
    #1
    1. Advertising

  2. "Stuart P" <> 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
     
    Victor Bazarov, Jul 1, 2003
    #2
    1. Advertising

  3. >
    > 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
     
    Kurt Krueckeberg, Jul 1, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Plamen Doykov

    Can't access internal class' members in 2.0

    Plamen Doykov, Oct 11, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    3,072
    Scott Allen
    Oct 12, 2005
  2. CoolPint
    Replies:
    8
    Views:
    1,027
    Jeff Schwab
    Dec 14, 2003
  3. hdixon
    Replies:
    3
    Views:
    673
    hdixon
    Jul 9, 2006
  4. Replies:
    2
    Views:
    856
    Noah Roberts
    Jun 9, 2006
  5. mrstephengross
    Replies:
    3
    Views:
    486
Loading...

Share This Page