473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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 [][].
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
2 1732
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
9,065 Expert Mod 8TB
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

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

Similar topics

11
by: John | last post by:
Hello all, I am trying to read in lines into a buffer from a file. Normally I would do this very low-level, but I have come to the conclusion I must stop doing everything the hard way. So, I...
10
by: Chiller | last post by:
Ok, this is a continuation of a problem I posted on an earlier thread. I've started another thread because my problem has progressed from the initial constructor problem into a general method...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
6
by: Chiller | last post by:
I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write...
3
by: John J | last post by:
I requested help with some code in a previous thread, as requested in the feedback, below are the .cpp and .h files for all three classes (Entry, Race and Yacht). The three classes are all...
2
by: Martin Jensen | last post by:
Hi I have a problem with Qt. My class definition is this: class Button : public QObject, public Tk_Object { Q_OBJECT public: Button() {} Button(Tk_Object &p); ~Button();
7
by: Stephan Rose | last post by:
Ok here is my scenario I have an interface called IScalar which describes a one dimensional number that has a certain unit of measurement. This interface is used to create multiple structures,...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.