From: Tony C. on 21 Dec 2009 21:07 Hi, I'm a vb programmer trying to convert to C++ and dealing with a rather steep learning curve. I want to pass an array of objects to my function. I was having trouble passing it by value, so to avoid copying issues I am trying to pass a pointer to it instead . The object is of type CPlanet my function declaration is: void CBiWheel::DrawTransit2NatalAspectLines(CClientDC *pDC, CPoint ptCenter, CPlanet &PInners[], CPlanet &Outers[]); the Array of CPlanets belongs to the Class CCalcedChart and is defined as: class CCalcedChart { public: CCalcedChart(void); ~CCalcedChart(void); CChart myChart; CPlanet myPlanets[11]; //10 planets double myHouses[13]; // 12 houses CPlanet myAsteroids[7]; }; //COuterChart and CInnerChart are of type CCalcedChart. //So my call to the function looks like: CBiWheel::DrawTransit2NatalAspectLines(&dlgDC,ptCtr, CInnerChart.myPlanets, COuterChart.myPlanets); //I'm gettting this output from the compiler: Error 6 error C2234: 'PInners' : arrays of references are illegal c:\project_test_area\1\astrocalcvc1\astrocalcvc1\BiWheel.h 41 AstroCalcVC1 Error 7 error C2234: 'Outers' : arrays of references are illegal c:\project_test_area\1\astrocalcvc1\astrocalcvc1\BiWheel.h 41 AstroCalcVC1 Error 27 error C2234: 'PInners' : arrays of references are illegal c:\project_test_area\1\astrocalcvc1\astrocalcvc1\BiWheel.h 41 AstroCalcVC1 Error 28 error C2234: 'Outers' : arrays of references are illegal c:\project_test_area\1\astrocalcvc1\astrocalcvc1\BiWheel.h 41 AstroCalcVC1 Error 29 error C2664: 'CBiWheel::DrawTransit2NatalAspectLines' : cannot convert parameter 3 from 'CPlanet (*)[11]' to 'CPlanet *[]' c:\PROJECT_TEST_AREA\1\AstroCalcVC1\AstroCalcVC1\BiWheel.cpp 162 AstroCalcVC1 Error 31 error C2234: 'PInners' : arrays of references are illegal c:\PROJECT_TEST_AREA\1\AstroCalcVC1\AstroCalcVC1\BiWheel.cpp 708 AstroCalcVC1 Error 32 error C2234: 'POuters' : arrays of references are illegal c:\PROJECT_TEST_AREA\1\AstroCalcVC1\AstroCalcVC1\BiWheel.cpp 708 AstroCalcVC1 Can anybody tell me what I'm doing wrong? Thanks, Tony C.
From: Tony C. on 21 Dec 2009 21:41 I got it straightened out... Thanks to Ivor Horton's Visual C++ 2008... lol! Tony C.
From: Igor Tandetnik on 21 Dec 2009 21:43 Tony C. <me(a)here.com> wrote: > I want to pass an array of objects to my function. I was having > trouble passing it by value, so to avoid copying issues I am trying > to pass a pointer to it instead . > > The object is of type CPlanet > > my function declaration is: > > void CBiWheel::DrawTransit2NatalAspectLines(CClientDC *pDC, CPoint > ptCenter, CPlanet &PInners[], CPlanet &Outers[]); CPlanet &PInners[] is an array of references. Arrays of references are illegal because references aren't objects (they don't have a size or an address and, technically, don't have to occupy any storage). If, as you say, you want an array of pointers, that would be CPlanet* PInners[]. However, I suspect you want something like this instead: void CBiWheel::DrawTransit2NatalAspectLines( CClientDC *pDC, CPoint ptCenter, CPlanet PInners[], size_t PInnersCount, CPlanet POuters[], size_t POutersCount); CPlanet PInners[] is equivalent to CPlanet* PInners. What you are really passing is the pointer to the first element of the array. You also need to pass the size: unlike VB, arrays don't know their own sizes. Better still, use vector to store a sequence of elements: #include <vector> using std::vector; class CCalcedChart { vector<CPlanet> myPlanets; }; void CBiWheel::DrawTransit2NatalAspectLines( CClientDC *pDC, CPoint ptCenter, const vector<CPlanet>& PInners, const vector<CPlanet>& POuters); vector is more like an array in VB. For example, you can ask it for its size with PInners.size(). Consider investing in a good textbook. "Accelerated C++" ( http://www.acceleratedcpp.com/ ) is often recommended for beginners. It skips over raw pointer manipulation and teaches proper C++ coding techniques from the start (not that knowing raw pointer manipulation is bad, but you can always get back to it when you know enough not to shoot yourself in the foot too often). -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Tony C. on 21 Dec 2009 22:30 Thanks Igor, Yes I'm looking into vectors (I tried CArrays and that was a mess!) and it took me a little while to figure out how to use them while copying objects,(must instantiate some storage space first) but I'm getting the hang of it now.. Thanks for the input..I will check out that book.. Tony C.
From: Igor Tandetnik on 21 Dec 2009 22:44 Tony C. <me(a)here.com> wrote: > Yes I'm looking into vectors (I tried CArrays and that was a mess!) > and it took me a little while to figure out how to use them while > copying objects,(must instantiate some storage space first) Not really: vector<CPlanet> planets; // planets.size() == 0 planets.push_back(CPlanet("my planet")); // planets.size() == 1 -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
|
Pages: 1 Prev: The case of the bizarre vanishing file? Next: std::set and std::multiset element mutability |