-3

I've created a class to simulate the behavior of vector of int, because every time I read or write some value, also a read and write counter should be incremented to keep track of its usage. I can’t change the code in main.cpp other than from the type “int” to the type “MyInt”, that's why I'm trying to overload the [] operator to read inside the brackets and pass the operation to a second one, using a wrapper class. The error I got is:

no match for ‘operator=’ (operand types are ‘MyInt’ and ‘int’)

so it looks like that the intercept of the assignment of the wrapper class doesn't work. I've also overloaded the "new []" operator for the dynamic declaration and it seems to work properly. Any suggestion?

Opaco
  • 3
  • 2
  • The problem is that the overloaded `operator=` that you've provided is for class `wrapper` and not `MyInt`. – Jason Oct 14 '22 at 14:01
  • Your indexing operator is never called, why would it be, you are indexing an array, not `MyInt` object. Try e.g. `V[3][0] = 8;` – Quimby Oct 14 '22 at 14:02
  • All of this usage of `new[]`, where does `delete[]` come into play? – PaulMcKenzie Oct 14 '22 at 14:06
  • Why do you want `new MyInt[10]` to construct one "vector" of 10 ints? Are you going to `#define int MyInt` or something equally obnoxious? – Caleth Oct 14 '22 at 15:47

2 Answers2

0

In your code:

    V = new MyInt[10];

V is an array of MyInt, so when you're trying to access the third index by V[3], you're not calling the overloaded operator[] but accessing the third element of the array, you can do this and see that the type of elem is MyInt and not MyInt::wrapper:

auto elem = V[3];

You can simply solve this by:

    (*V)[3] = 3;
    // or
    V[0][3] = 3;

But this is a terrible way to make a vector, you should just use std::vector or re-design it.

thedemons
  • 1,139
  • 2
  • 9
  • 25
  • Thanks! With V[0][3] it works, but is there a way to perform the same access using just "V[3]" so I don't have to write V[0][3]? I would like to simulate the behavior of an array of int being allocated while in reality it's not. Unfortunately, I can't change the code in main other than from the type "Int" to "MyInt". – Opaco Oct 14 '22 at 14:10
  • I'm not aware of anything like that, maybe you shouldn't use the new[] operator, just pass the size to MyInt ctor and allocate the array there instead. Don't overcomplicate things that could be done simpler – thedemons Oct 14 '22 at 14:20
  • Unfortunately, my class should work in future as a library, finding and replace in code every vector from "int" to "MyInt". The only changes I can make other than that are in "MyInt.h", that's why. If you have any suggestion will be really appreciated. Thanks. – Opaco Oct 14 '22 at 14:37
0

The problem is that the overloaded operator= that you've provided is for class wrapper and not MyInt. Now, the type of the expression V[3] is MyInt and since there is no MyInt::operator=, you get the mentioned error for V[3] = 8.

To solve this, change V[3] to (*V)[3] so that now, the type of the expression *V is MyInt and that of (*V)[3] is wrapper and since there is an MyInt::wrapper::operator= available, this will work.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Thanks! Is there a way to leave V[3] in main.cpp as it is and change the code in "MyInt.h" to make it work somehow? – Opaco Oct 14 '22 at 14:14
  • @Opaco You're welcome. I don't think so. Though can possible be a way if you overload `operator*` for `MyInt` but that'll make the code worse. – Jason Oct 14 '22 at 14:26
  • Thanks again. How would you overload the operator* to make it work? Do you have any suggestion? – Opaco Oct 14 '22 at 14:32
  • @Opaco Basically, the overloaded `MyInt::operator*` can return a `wrapper` object so that `wrapper::opeartor[]` can be used. Something like that. Not entirely sure if that is possible but i just wanted to say it so that you can try it and somehow make it work. But doing that would make the code very unintuitive as `MyInt::operator*` should in theory return a `MyInt`. – Jason Oct 14 '22 at 14:35
  • And "MyInt::operator*" can return a wrapper object? You are suggesting something like test& operator*( ){ return wrapper(*this); } – Opaco Oct 14 '22 at 15:07
  • @Opaco Yes, except that the return type would be `wrapper&` instead of `test&`. – Jason Oct 14 '22 at 15:20
  • @Opaco You're welcome. I noticed that there are 2 answers posted to your new question one(or both) of which should solve your issue. – Jason Oct 14 '22 at 15:48
  • Which one? Unfortunately I think they have misunderstood the question (probably not well formulated) since I need to return a wrapper object from indirection overload not to overload equals or square bracket operator. Could you help? Thanks – Opaco Oct 14 '22 at 15:50
  • @Opaco I am talking about [this](https://stackoverflow.com/questions/74071540/c-overload-indirection-operator-to-return-another-class-object) one. Currently, i'm quite busy and also it's around midnight here so probably go to sleep soon. So won't be able to help. – Jason Oct 14 '22 at 15:56