Hi, so I'm a total beginner and am having trouble trying to overload the insertion operator. When I try it tells me "error: no match for ‘operator>>’ (operand types are ‘std::istream’ {aka ‘std::basic_istream’} and ‘int’)"
Here is the header:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
class myArray {
public:
myArray(int len = 0);
intoperator[](int i) { return a[i]; }
friend istream& operator>> (istream &in, myArray &x);
private:
int arrayLen;
int a[];
};
What do YOU think line 11 is doing in the 2nd one?
How big do you think that A array is?
you cannot cin an array. you have to loop and read one by one.
and C style arrays need a fixed size at compile time; the only way around this is a pointer of some sort (whether done for you or not).
edit, misread what you did, the >> is fine apart from trying to read and write an array.
The first issue is that your array a is not specified as having any size. The size of arrays, whether in a class or not, must be known at compile time.
The second issue is second snippet, line 9: myArray::istream& is not a type. You meant to just write istream&.
The third issue is second snippet, line 11: You are calling >> on an array (a). Presumably you meant to call something more like:
1 2
in >> x.a[arrayLen];
arrayLen++;
You should also be thinking about bounds checking, e.g. (if arrayLen == maxSize) { don't add to array }. This is assuming you have some notion of "MaxSize" (see first issue).
person5273, your code that you have shown does not have problems, assuming it's being successfully compiled/linked by however you are building it. I was able to copy and paste it into cpp.sh as one file and compile it (after removing the #include "myArray.h"), after adding a main function. So the problem exists in part of the code you are not showing.