Re: cannot bind to ostream
Quote:
Originally Posted by underfor
Everything seemed to be fine when I tested out my integers but when I got to my double and char classes the compiler gave me this error:
Your tests were not consistent. Observe carefully what you wrote for integers:
Code:
array < int > integers(3);
cout << "After instantiation, array: " << endl;
integers.getArray();
integers.setArray(0,8);
integers.setArray(1,4);
integers.setArray(2,9);
cout << "After initialization, array: " << endl;
integers.getArray();
integers.inputArray();
integers.getArray();
Great. You called member functions like getArray and inputArray. No problem there. Now, compare with what you wrote for doubles:
Code:
cout << "\nTESTING DOUBLES\n";
array < double > dbls(2);
dbls.setArray(0,0);
dbls.getArray();
cout << "After instantiation, array:\n" << dbls << endl;
dbls.inputArray();
cout << "After initialization, array:\n" << dbls << endl;
dbls.getArray();
Notice that you attempted to print dbls twice with an overloaded operator<< that you did not declare, much less implement!
Likewise, observe for chars:
Code:
cout << "After instantiation, array:\n" << chrs << endl;;
cin >> chrs;
cout << "After initialization, array:\n" << chrs << endl;;
You attempted to print chrs twice with an overloaded operator<< that you did not declare, much less implement, and in between you attempted to read into chrs using an overloaded operator>> that you likewise did not declare, much less implement!
Quote:
Originally Posted by underfor
I tried googling it but a lot of the answers said it was the compiler's fault and I needed a work around but I'm not sure how to do that.
No, it is not your compiler's fault. It is your fault for not overloading operator<< and operator>> (or for attempting to use them).
- Get rid of getArray in favour of overloading operator<< for std::ostream. I think that getArray is not a good name for such a function anyway as usually people would expect a function prefixed with "get" to return rather than print output.
- Keep inputArray, but turn it into a non-member non-friend function. You might be tempted to overload operator>> instead, but your inputArray function does interactive input, whereas it is more typical for an overloaded operator>> for std::istream to read input as if it comes from a file. If you take this advice, you should get rid of your attempted use of operator>> in main.
- setArray should be renamed to setElement.
- You should match new[] with delete[]. In this case, a first step is to define the destructor.
By the way, since C++11, there is a container class template named array in the standard library. This means that if you want to name your class template array, you need to be careful to avoid a possible name collision, e.g., by only placing using namespace std (or using std::array) in a restricted scope where your own array class template is not used, and perhaps placing your own array class template in your own namespace.
Re: cannot bind to ostream
Nice Answer Laserlight. Very professional.