943,514 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1042
  • C++ RSS
Feb 23rd, 2009
0

Problem on Returning Reference to Template Object

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Feb 23rd, 2009
0

Re: Problem on Returning Reference to Template Object

change the reference to a pointer and see if that solves the problem.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,945 posts
since Aug 2005
Feb 24th, 2009
0

Re: Problem on Returning Reference to Template Object

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Project compiles fine in Windows, but gives undefined references in Linux?? Huh?
Next Thread in C++ Forum Timeline: socket c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC