Convert from CObject to inherited class
-
I'm trying to use CObList with a class I created. The class I created I inherited CObject. class CFAFRecord : public CObject {/*data*/} I'm able to create this and insert it into the CObList. But when I try to access the list with m_variable.GetNext(m_curPosition); and I try to assign it to a CFAFRecord object, I get the error, 'cannot convert 'class CObject' to 'class CFAFRecord'. It won't let me typecast it. It looks like it's not recognizing that CFAFRecord is a CObject. Any help here? Danny The stupidity of others amazes me!
-
I'm trying to use CObList with a class I created. The class I created I inherited CObject. class CFAFRecord : public CObject {/*data*/} I'm able to create this and insert it into the CObList. But when I try to access the list with m_variable.GetNext(m_curPosition); and I try to assign it to a CFAFRecord object, I get the error, 'cannot convert 'class CObject' to 'class CFAFRecord'. It won't let me typecast it. It looks like it's not recognizing that CFAFRecord is a CObject. Any help here? Danny The stupidity of others amazes me!
First bit of advice is to drop
CObList
and use STL'sstd::list
instead. If you can not do that and have compeling reasons to useCObList
maybe you could post some code showing how you are trying to cast the returned pointer.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
-
I'm trying to use CObList with a class I created. The class I created I inherited CObject. class CFAFRecord : public CObject {/*data*/} I'm able to create this and insert it into the CObList. But when I try to access the list with m_variable.GetNext(m_curPosition); and I try to assign it to a CFAFRecord object, I get the error, 'cannot convert 'class CObject' to 'class CFAFRecord'. It won't let me typecast it. It looks like it's not recognizing that CFAFRecord is a CObject. Any help here? Danny The stupidity of others amazes me!
Although I have used MFC's template classes for years in the past, I second the advice above. Take a look at stl. John
-
Although I have used MFC's template classes for years in the past, I second the advice above. Take a look at stl. John
John M. Drescher wrote:
Although I have used MFC's template classes for years in the past, I second the advice above. Take a look at stl
Any Preculiar reason for that ?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
I'm trying to use CObList with a class I created. The class I created I inherited CObject. class CFAFRecord : public CObject {/*data*/} I'm able to create this and insert it into the CObList. But when I try to access the list with m_variable.GetNext(m_curPosition); and I try to assign it to a CFAFRecord object, I get the error, 'cannot convert 'class CObject' to 'class CFAFRecord'. It won't let me typecast it. It looks like it's not recognizing that CFAFRecord is a CObject. Any help here? Danny The stupidity of others amazes me!
try this CFAFRecord *my_list =(CFAFRecord*) m_variable->GetNext( m_curPosition); regards Rajesh
-
try this CFAFRecord *my_list =(CFAFRecord*) m_variable->GetNext( m_curPosition); regards Rajesh
Your suggestion does not work. I've switched to using CObArray, but the same sort of thing happens. m_variable is not a pointer to the CObArray, so I don't need to dereference it using ->, I need to access it using . The problem really occurs, I guess, when I try to access one of the variables of my_list after this statement, using my_list->variable. Danny The stupidity of others amazes me!
-
First bit of advice is to drop
CObList
and use STL'sstd::list
instead. If you can not do that and have compeling reasons to useCObList
maybe you could post some code showing how you are trying to cast the returned pointer.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
Thanks for the advice, but I'd really like to stick with MFC in this case. I switched to using CObArray, but something very similar happens. I've found I can do
CFAFRecord * my_record = (CFAFRecord *)my_variable.GetAt(index);
but on the next line I try,my_record->member_variable;
and I get an access violation. Danny The stupidity of others amazes me! -
Thanks for the advice, but I'd really like to stick with MFC in this case. I switched to using CObArray, but something very similar happens. I've found I can do
CFAFRecord * my_record = (CFAFRecord *)my_variable.GetAt(index);
but on the next line I try,my_record->member_variable;
and I get an access violation. Danny The stupidity of others amazes me!You are obviously doing something wrong with code you have not posted here so far. I slapped together a quick MFC console app to show you what I did to make it work
/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;
using namespace std;
class CMyObject : public CObject
{
public:
CMyObject() { i = 0; d = 0.0; }
int i;
double d;
};CObList ObList;
void DumpList()
{
if (ObList.GetCount() == 0)
return;POSITION pos = ObList.GetHeadPosition(); while(pos) { CMyObject \*p = (CMyObject \*)ObList.GetNext(pos); cout << p->i << " " << p->d << endl; }
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;// initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs cerr << \_T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; } else { // TODO: code your application's behavior here. CString strHello; strHello.LoadString(IDS\_HELLO); cout << (LPCTSTR)strHello << endl; } // Fill the CObList with some data for (int x = 1; x < 10; ++x) { CMyObject \*p = new CMyObject; p->i = x; p->d = x + x / 10.0; ObList.AddTail(p); } DumpList(); return nRetCode;
}
Not perfect, leaks memory, but it should give you an idea.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
-
John M. Drescher wrote:
Although I have used MFC's template classes for years in the past, I second the advice above. Take a look at stl
Any Preculiar reason for that ?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
Two main reasons. Power and Portability. stl offers you signifigantly more power than the MFC template classes. The ability to use algorithms on your collection classes is a very nice feature. http://oopweb.com/CPP/Documents/STL/VolumeFrames.html?/CPP/Documents/STL/Volume/prw432.htm[^] There are also way more different template (hash_maps, stacks, queues, sets ...) types than are offered with the MFCTemplates. Changing from one container type to a second usually does not require many changes at all as the iterators code is all the same. Inserts and sorting may vary though. And portability, besides being able to use stl code on different operating systems you can also use it with your win32 or atl projects without any changes to the stl code. John
-
try this CFAFRecord *my_list =(CFAFRecord*) m_variable->GetNext( m_curPosition); regards Rajesh
show ur code what u exactly doing?