virtual function crash
-
I have an abstract class like:
class CMyAbstract
{
public:
virtual void Func(char *Param) = 0;
};Then I have a class that uses it as its base class.
class CMyCLass : public CMyAbstract
{
public:
virtual void Func(char *Param);
};In an implementation file
CMyAbstract *pClass = new CMyClass;
pClass->Func("yada");This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...
-
I have an abstract class like:
class CMyAbstract
{
public:
virtual void Func(char *Param) = 0;
};Then I have a class that uses it as its base class.
class CMyCLass : public CMyAbstract
{
public:
virtual void Func(char *Param);
};In an implementation file
CMyAbstract *pClass = new CMyClass;
pClass->Func("yada");This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...
Does it crash in the constructor or destructor? Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
-
I have an abstract class like:
class CMyAbstract
{
public:
virtual void Func(char *Param) = 0;
};Then I have a class that uses it as its base class.
class CMyCLass : public CMyAbstract
{
public:
virtual void Func(char *Param);
};In an implementation file
CMyAbstract *pClass = new CMyClass;
pClass->Func("yada");This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...
Hope you have implemented the function? Else being a virtual function it'll try and call the base function which points to NULL. Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
-
Hope you have implemented the function? Else being a virtual function it'll try and call the base function which points to NULL. Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
yes the function is implemented. Steve Not all who wander are lost...
-
Does it crash in the constructor or destructor? Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
No it does not. Steve Not all who wander are lost...
-
I have an abstract class like:
class CMyAbstract
{
public:
virtual void Func(char *Param) = 0;
};Then I have a class that uses it as its base class.
class CMyCLass : public CMyAbstract
{
public:
virtual void Func(char *Param);
};In an implementation file
CMyAbstract *pClass = new CMyClass;
pClass->Func("yada");This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...
Steve Severance wrote: CMyAbstract *pClass = new CMyClass; Shouldn't it be: CMyClass* pClass = new CMyClass; Neville Franks, Author of ED for Windows. www.getsoft.com
-
I have an abstract class like:
class CMyAbstract
{
public:
virtual void Func(char *Param) = 0;
};Then I have a class that uses it as its base class.
class CMyCLass : public CMyAbstract
{
public:
virtual void Func(char *Param);
};In an implementation file
CMyAbstract *pClass = new CMyClass;
pClass->Func("yada");This crashes as long as its a virtual function. Why is this. Don't worry about omitted thing like constructors. Steve Not all who wander are lost...
-
Did you Copy/Paste the example code from your project? If so, check the class names: In the class definition CMyCLass is written with a uppercase L, the implementation fragment has CMyClass with a lowercase l.
The error is runtime(Access Violation) not compile time. Here is actual source. Header
class CInputCommandMap
{
public:
CInputCommandMap();
~CInputCommandMap();void LoadCommandMap(char \*file); virtual void ProcessInput(char \*keys, DIMOUSESTATE2 \*mouse) = 0; CInputCommand m\_keyA; CInputCommand m\_keyB; CInputCommand m\_keyC; More Keys...
};
class CDefaultKeyMap : public CInputCommandMap
{
public:
CDefaultKeyMap(){}
~CDefaultKeyMap(){}void ProcessInput(char \*keys, DIMOUSESTATE2 \*mouse);
};
ProcessInput is overridden and contains the actual implentation. LoadCommandMap is
void LoadCommandMap(CInputCommandMap *map){m_pCommandMap = map;}
I use it like this.
pDefaultCommandMap = new CDefaultKeyMap; pDefaultCommandMap->LoadCommandMap("default.key"); pInput->LoadCommandMap(pDefaultCommandMap); Code...In an update function that is in the main input class if(m\_pCommandMap) m\_pCommandMap->ProcessInput((char \*)m\_Keys,&m\_MouseState);
Thanks once again for all you guys help. Steve Not all who wander are lost...
-
The error is runtime(Access Violation) not compile time. Here is actual source. Header
class CInputCommandMap
{
public:
CInputCommandMap();
~CInputCommandMap();void LoadCommandMap(char \*file); virtual void ProcessInput(char \*keys, DIMOUSESTATE2 \*mouse) = 0; CInputCommand m\_keyA; CInputCommand m\_keyB; CInputCommand m\_keyC; More Keys...
};
class CDefaultKeyMap : public CInputCommandMap
{
public:
CDefaultKeyMap(){}
~CDefaultKeyMap(){}void ProcessInput(char \*keys, DIMOUSESTATE2 \*mouse);
};
ProcessInput is overridden and contains the actual implentation. LoadCommandMap is
void LoadCommandMap(CInputCommandMap *map){m_pCommandMap = map;}
I use it like this.
pDefaultCommandMap = new CDefaultKeyMap; pDefaultCommandMap->LoadCommandMap("default.key"); pInput->LoadCommandMap(pDefaultCommandMap); Code...In an update function that is in the main input class if(m\_pCommandMap) m\_pCommandMap->ProcessInput((char \*)m\_Keys,&m\_MouseState);
Thanks once again for all you guys help. Steve Not all who wander are lost...
You've got the right idea, but you've got bugs in both your interfaces in your imnplementations. The implementation bug is what crashes it. Sorry, can't help without _full_ definition. The interface bug is that a class that is intended to be inherited from, especially if it contains data (!), should (almost) always have a virtual destructor. To give you a hint: Your code is probably overwriting some memory it doesn't own.