How to do new CArray???
-
Hi Guys, I am having a problem creating a pointer to a CArray structure;
CArray<CBackground*, CBackground*>* m_paryBackground; int nNewSize = 2; m_paryBackground = (CArray<CBackground*, CBackground*>*)malloc(nNewSize * sizeof(CArray<CBackground*, CBackground*>*)); m_paryBackground[0] = new CArray<CBackground*, CBackground*>; m_paryBackground[1] = new CArray<CBackground*, CBackground*>;
This does not work for some reason. I get a compile time error saying it cannot cast to the m_paryBackground variable. Does anyone know how I can do this properly? I'm using MFC 7 visual studio 2005 Cheers, Greg -
Hi Guys, I am having a problem creating a pointer to a CArray structure;
CArray<CBackground*, CBackground*>* m_paryBackground; int nNewSize = 2; m_paryBackground = (CArray<CBackground*, CBackground*>*)malloc(nNewSize * sizeof(CArray<CBackground*, CBackground*>*)); m_paryBackground[0] = new CArray<CBackground*, CBackground*>; m_paryBackground[1] = new CArray<CBackground*, CBackground*>;
This does not work for some reason. I get a compile time error saying it cannot cast to the m_paryBackground variable. Does anyone know how I can do this properly? I'm using MFC 7 visual studio 2005 Cheers, Greg -
Have you read the documentation for how to use a CArray? The CArray class is an array management class, not a simply C array. Read the docs and use the class' member functions such as SetSize() and SetAt().
Yep I have, I am not merely trying to use a CArray I am trying to create a pointer to an array of CArray's. It won't let me do it using malloc for some reason.
-
Yep I have, I am not merely trying to use a CArray I am trying to create a pointer to an array of CArray's. It won't let me do it using malloc for some reason.
malloc is a C function, not a C++ function. It simply allocates a block of memory, nothing more. You need to use new which allocates a block of memory and then calls the class' c'tor.
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!
-
Hi Guys, I am having a problem creating a pointer to a CArray structure;
CArray<CBackground*, CBackground*>* m_paryBackground; int nNewSize = 2; m_paryBackground = (CArray<CBackground*, CBackground*>*)malloc(nNewSize * sizeof(CArray<CBackground*, CBackground*>*)); m_paryBackground[0] = new CArray<CBackground*, CBackground*>; m_paryBackground[1] = new CArray<CBackground*, CBackground*>;
This does not work for some reason. I get a compile time error saying it cannot cast to the m_paryBackground variable. Does anyone know how I can do this properly? I'm using MFC 7 visual studio 2005 Cheers, GregYou should never use malloc in C++. You should also prefer to use std::vector over CArray, as it is standard and it is more flexible/powerful.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
malloc is a C function, not a C++ function. It simply allocates a block of memory, nothing more. You need to use new which allocates a block of memory and then calls the class' c'tor.
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!
ok, but I need to dynamically allocate memory so I wanted to be able to use realloc. Is there a realloc equivilent for new? Thanks
-
ok, but I need to dynamically allocate memory so I wanted to be able to use realloc. Is there a realloc equivilent for new? Thanks
-
Hi Guys, I am having a problem creating a pointer to a CArray structure;
CArray<CBackground*, CBackground*>* m_paryBackground; int nNewSize = 2; m_paryBackground = (CArray<CBackground*, CBackground*>*)malloc(nNewSize * sizeof(CArray<CBackground*, CBackground*>*)); m_paryBackground[0] = new CArray<CBackground*, CBackground*>; m_paryBackground[1] = new CArray<CBackground*, CBackground*>;
This does not work for some reason. I get a compile time error saying it cannot cast to the m_paryBackground variable. Does anyone know how I can do this properly? I'm using MFC 7 visual studio 2005 Cheers, GregSomething like this?
size_t nNewSize = 2;
CArray<CBackground*, CBackground*>* m_paryBackground = new CArray<CBackground*, CBackground*>[nNewSize];In the future, you should include the compiler error you are getting so we don't have to guess.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?