I am having problems here if I want to check if eerste
points to nothing I get
Blockq开发者_StackOverflow社区uote
Unhandled exception at 0x003921c6 in Bank.exe: 0xC0000005: Access violation reading location 0xccccccd0.
and I am kinda wondering why he justs skips the if statement or doens't stop when the object eerste
points to nothing
Bank::Bank()
{
LijstElement *eerste = NULL;
LijstElement *laatste = NULL;
}
Rekening * Bank::getRekening(int rekNr)
{
if(NULL != eerste)
{
LijstElement *nummer = eerste;
while(nummer->volgende!= NULL)
{
Rekening *een = nummer->getRekening();
if(een->getRekNr()==rekNr)
{
return een;
}
else
{
nummer = nummer->volgende;
}
}
}
return NULL;
}
I think you have to change
Bank::Bank()
{
eerste = NULL;
laatste = NULL;
}
because they are probably declared in your class as member variables and you're declaring them as local variables.
As Fred Larson proposed, you can also use initialization lists.
Bank::Bank() : eerste(NULL), laatste(NULL)
{
//whatever else you are planning to do in your constructor
}
eerste and laatste are not your Bank class's member variables, but the local variables of the contructor (even if members are defined, the local vars override them in the constructor - so the actual member variables remain unassigned)
I'd propose to use a naming convention (add a m_ prefix to member variables) and a simple initialization list :
class Bank
{
public:
Bank() : m_eerste(NULL), m_laatste(NULL)
{}
private:
LijstElement *m_eerste;
LijstElement *m_laatste;
};
The syntax in your constructor suggests eerste
and laatste
are not member variables (and are thus illegal to reference in getRekening
.)
Could you please post correct code including the portion in your header file?
Edit: Other than that, it looks like you're trying to use an uninitialized pointer. (Which points to nothing but not NULL, either.)
精彩评论