public member cannot be accessed in functions

Programming, for all ages and all languages.
Post Reply
Moons

public member cannot be accessed in functions

Post by Moons »

Hello every one,

I presently have a struct with all kinds of information (bool, char*...) in the public of MyClass (main class). I have a function which loads the information which is called in the constructor of MyClass. When I access the information of the struct in the constructor, every thing is loaded correctly and I can access them. When I try to access the same information from a function there is access violation and 0x024... referenced at 0x927, memory could not be read problems. To trick the computer, I re-loaded the information calling the loading function in the function where I needed it. In that case, the information is loaded correctly but when I want to use it, it crashes!!! It could be a problem of memory which is used the first time and then new things are written over it or something like that but I don't know how to check that and I don't really assign any space anywhere and the free are always used...Help!!

Code snipet:
MyClass.h
struct Pers
bool t;
char* n;

MyClass.cpp
constructor
LoadInfo();
Pers.t; //works
Pers.n; //works

functions
foo()
Pers.t; //doesn't work!!!

Thanx
Rahman

Re:public member cannot be accessed in functions

Post by Rahman »

you can't call another function from a constructor. constructor is a special member function in the name of the class which is meant to just initialize the member variables.
zloba

Re:public member cannot be accessed in functions

Post by zloba »

Rahman
you can't call another function from a constructor. constructor is a special member function in the name of the class which is meant to just initialize the member variables.
that is simply not true, no offence, but i'm fairly certain beause i've been doing it all the time. there may be quirks, though.
Moons
if you could provide a minimal example of the actual code that demonstrates the problem, that would be helpful to find the problem.
Post Reply