Problem on Returning Reference to Template Object

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Problem on Returning Reference to Template Object

 
0
  #1
Feb 23rd, 2009
Hi,
I am using VC++6, MFC Application

I am having the member variable - CArray < Coordinates, Coordinates & > mCoordinates. Coordinates is a structure, contains x, y, and z. When i return reference to this object, there is no problem. but When i assign to some reference i get problem.

The Code is

  1. struct Coordinates{
  2. int x, y, z;
  3. };
  4. class A{
  5. private:
  6. CArray < Coordinates, Coordinates & > mCoordinates
  7. public: CArray < Coordinates, Coordinates & > & GetCoordinates() {
  8. return mCoordinates;
  9. }
  10. };
  11.  
  12. class B {
  13. public:
  14. void Print () {
  15. A objA;
  16. CArray < Coordinates, Coordinates & > &ref = objA.GetCoordinates ();
  17. }
  18. }

In Class B's Print () function, i am getting problem when assign the coordinates. Compiler says different level of indirection. Anyone suggest me how to solve this.
KSG
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,363
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem on Returning Reference to Template Object

 
0
  #2
Feb 23rd, 2009
change the reference to a pointer and see if that solves the problem.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: Problem on Returning Reference to Template Object

 
0
  #3
Feb 24th, 2009
I changed the reference as pointer that also not working. Finally i found the solution. I just inherit a class from CArray <>, that solves the problem.

  1. struct Coordinates{
  2. int x, y, z;
  3. };
  4. class CoordinateArray : public CArray < Coordinates, Coordinates & > {};
  5. class A{
  6. private:
  7. CoordinateArray mCoordinates
  8. public: CoordinateArray & GetCoordinates() {
  9. return mCoordinates;
  10. }
  11. };
  12.  
  13. class B {
  14. public:
  15. void Print () {
  16. A objA;
  17. CoordinateArray &ref = objA.GetCoordinates ();
  18. }
  19. }
Last edited by selvaganapathy; Feb 24th, 2009 at 9:51 am.
KSG
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC