Let's say I have a class PArr which stores an array of various classes StringWrapper, FloatWrapper, IntWrapper- all of them derive from a base class Wrapper.
It's PArr class:
class PArr{
private:
Wrapper** _wrappers;
int _size;
public:
PArr(int size);
Wrapper& operator[](int index);
};
And here's the implementation of this class
#include "PArr.h"
PArr::PArr(int size){
_size = size;
_wrappers = new Wrapper*[_size];
}
Wrapper& PArr::operator[](int index){
return _wrappers[index];
}
And here's the class for example FloatWrapper
#pragma once
#include "Wrapper.h"
class FloatWrapper : public Wrapper{
private:
float* _floatPtr;
public:
FloatWrapper(float number);
};
My main method looks like this:
int main() {
PArr a(3);
a[0] = new FloatWrapper(0.1);
}
I get an error: "no match for 'operator=' (operand types are 'Wrapper' and 'FloatWrapper*')
What am I doing wrong?
operator[]returns aWrapper &, but_wrappers[index]is of typeWrapper *. The mistake you're making is assuming that references and pointers are interchangeable. They are not.