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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markjan
    New Member
    • Dec 2009
    • 1

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

    I have a problem with classes and structures in classes (C++) I have to overload operator [][].
    Code:
    class Data {
    public:
     
     	class Proxy {               //for overload [][] operator
        		Data& _a;
       		int _i;
      		public:
        		Proxy(Data& a, int i) : _a(a), _i(i) {}
    
    Data & operator[](int j) {    
    		_a.x_=_i; 
    		_a.y_=j;
    		return _a;
    		}
     }; // end of class Proxy
      
    Proxy operator[](int i) { 
    	  return Proxy(*this, i); 
     }
    
    Data (); 
    Data (const Data &);
    ~ Data ();
    
    private:
    int n, m, x_, y_;  
    double* s;     
    
    };
    Everything is OK. It’s working excellent. But the problem is when I’m moving it into struct in 14 and 15 line.

    Code:
    class Data{
      struct Detail;
      Detail* data;
     
    public:
    
    class Proxy {               //for overload [][] operator
        		Data& _a;
       		int _i;
      		public:
        		Proxy(Data& a, int i) : _a(a), _i(i) {}
    
    Data & operator[](int j) {    
    		_a.data->x_=_i;       // it’s not seeing structure !
    		_a.data->y_=j;
    		return _a;
    		}
     }; // end of class Proxy
      
    Proxy operator[](int i) { 
    	  return Proxy(*this, i); 
     }
    
    Data();
    Data(const Data&);
    ~Data();
    
    };
    
    struct Data::Detail
    {
      double* s;
      int n, m, y_, x_;
      unsigned int n;
    };
    Please help.
  • akdemirc
    New Member
    • Sep 2008
    • 26

    #2
    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..

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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.

      Comment

      Working...