By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Problem with classes and structures in classes and overload operator [][]

P: 1
I have a problem with classes and structures in classes (C++) I have to overload operator [][].
Expand|Select|Wrap|Line Numbers
  1. class Data {
  2. public:
  3.  
  4.      class Proxy {               //for overload [][] operator
  5.             Data& _a;
  6.            int _i;
  7.           public:
  8.             Proxy(Data& a, int i) : _a(a), _i(i) {}
  9.  
  10. Data & operator[](int j) {    
  11.         _a.x_=_i; 
  12.         _a.y_=j;
  13.         return _a;
  14.         }
  15.  }; // end of class Proxy
  16.  
  17. Proxy operator[](int i) { 
  18.       return Proxy(*this, i); 
  19.  }
  20.  
  21. Data (); 
  22. Data (const Data &);
  23. ~ Data ();
  24.  
  25. private:
  26. int n, m, x_, y_;  
  27. double* s;     
  28.  
  29. };
  30.  
Everything is OK. Itís working excellent. But the problem is when Iím moving it into struct in 14 and 15 line.

Expand|Select|Wrap|Line Numbers
  1. class Data{
  2.   struct Detail;
  3.   Detail* data;
  4.  
  5. public:
  6.  
  7. class Proxy {               //for overload [][] operator
  8.             Data& _a;
  9.            int _i;
  10.           public:
  11.             Proxy(Data& a, int i) : _a(a), _i(i) {}
  12.  
  13. Data & operator[](int j) {    
  14.         _a.data->x_=_i;       // itís not seeing structure !
  15.         _a.data->y_=j;
  16.         return _a;
  17.         }
  18.  }; // end of class Proxy
  19.  
  20. Proxy operator[](int i) { 
  21.       return Proxy(*this, i); 
  22.  }
  23.  
  24. Data();
  25. Data(const Data&);
  26. ~Data();
  27.  
  28. };
  29.  
  30. struct Data::Detail
  31. {
  32.   double* s;
  33.   int n, m, y_, x_;
  34.   unsigned int n;
  35. };
  36.  
Please help.
Dec 5 '09 #1
Share this Question
Share on Google+
2 Replies


P: 26
i think the problem is that you are forward declaring the structure Detail and try to use it without actually declaring.. Try including the file in which it is actually declared to be able to use its members and assign them..
Dec 5 '09 #2

Banfa
Expert Mod 5K+
P: 8,916
Strictly speaking there is no operator[][]. I see what you are trying to do, overload operator[] in a class and a nested class. My initial reaction to that is skepticism as to whether it can be done and even if it can be done doubt that it is a good idea.

What is wrong with a simple method that takes 2 parameters and not having a nested class at all?

Anyway assuming what you are trying to do is possible and that there is a good reason for doing it like that your description of you problem lacks detail. In fact it is "I have a problem".

Are you getting unexpected behaviour at run time? If so what was the expected behaviour, what was the unexpected behaviour, what was the input data.

Or were you getting compiler or linker errors (and other diagnostics)? If so what were they? Copy and paste them into a post with the line numbers translated to the line numbers of the code you have posted.
Dec 5 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.