hey all,
i'm working on a very simplistic example to revive my cpp skills for an up coming work placement. however i'm running into what seems an inconsistant error using ostream (cout)... have a look at the following code in the main()...
:: main.cpp ::
#include <iostream>
#include "IntArray.h"
using namespace std;
int main()
{
IntArray a(7);
for(int i = 0; i < a.getNumElements(); i++) a[i] = i * i;
cout << a << endl << endl;
IntArray b(4);
for(int i = 0; i < b.getNumElements(); i++) b[i] = i - (2 * i) * i;
cout << b << endl << endl;
// THIS WORKS
b = b + a + b + a;
cout << b << endl;
// BUT THIS DOESN'T
cout << (b + a) << endl;
}
the compiler error is as follows: /home/allsey/Programming/testa/main.cpp|19|error: no match for ‘operator<<’ in ‘std::cout << IntArray::operator+(const IntArray&)(((const IntArray&)((const IntArray*)(& a))))’|
it seems from this error, that ostream is looking for a method to print out the operator as aposed to its return type... does any one know what is going on here? - i'm also open to any sugestions about my style of implementing these operators and whether there is any room for improvement.
here is the rest of the code:
:: IntArray.h ::
#ifndef INTARRAY_H
#define INTARRAY_H
#include <stdexcept>
class IntArray
{
friend std::ostream & operator<<(std::ostream & toStream, IntArray & theArray);
public:
IntArray(int size); // std constructor
IntArray(const IntArray & other); // copy constructor
IntArray(const IntArray &first, const IntArray &last); // concat constructor
~IntArray(); // destructor
int getNumElements() const;
int & operator[](int index);
IntArray operator-(IntArray & toNegate); // TO BE IMPLEMENTED
IntArray operator+(const IntArray & toConcat); // concat operator
IntArray operator=(const IntArray & toAssign);
private:
int * data;
int numElements;
};
#endif
:: IntArray.cpp ::
#include <ostream>
#include "IntArray.h"
IntArray::IntArray(int size)
{
data = new int[size];
numElements = size;
for(int i = 0; i < size; i++) data[i] = 0;
}
IntArray::IntArray(const IntArray & other)
{
data = new int[other.numElements];
numElements = other.numElements;
for(int i = 0; i < other.numElements; i++) data[i] = other.data[i];
}
IntArray::IntArray(const IntArray &first, const IntArray &last) // concat constructor
{
data = new int[first.numElements + last.numElements];
numElements = first.numElements + last.numElements;
for(int i = 0; i < first.numElements; i++)
{
data[i] = first.data[i];
}
for(int i = 0; i < last.numElements; i++)
{
data[i + first.numElements] = last.data[i];
}
}
IntArray::~IntArray()
{
delete []data;
}
int IntArray::getNumElements() const
{
return numElements;
}
int & IntArray::operator[](int index)
{
if(index < 0 || index > (numElements - 1)) throw std::out_of_range("bad index");
else return data[index];
}
IntArray IntArray::operator+(const IntArray & toConcat) // concat operator
{
return IntArray(*this,toConcat);
}
IntArray IntArray::operator=(const IntArray & toAssign) // concat operator
{
delete []data;
data = new int[toAssign.numElements];
numElements = toAssign.numElements;
for(int i = 0; i < toAssign.numElements;i++)
{
data[i] = toAssign.data[i];
}
return *this;
}
std::ostream & operator<<(std::ostream & toStream, IntArray & theArray)
{
toStream << "{";
for(int i = 0; i < theArray.numElements - 1; i++) toStream << theArray.data[i] << ", ";
toStream << theArray.data[theArray.numElements - 1] << "}";
return toStream;
}