Almost there! Got the assignment operator working right now.. for some reason, the == comparison operator (and I would imagine the < and > comparison operators would be the same) are having issues when trying to pass them an array class with char. They do work with ints, though.
I've compared how I implemented the overloaded ==, >, and < operators to some other examples I've found in books and online and I fail to see where I went wrong. The only thing that comes to mind (because of the error that VC++ is complaining about) is to overload the overloaded operators (if that's even possible) to accomodate chars. This seems to defeat the purpose of templated classes if I have to have multiple overloaded operators/functions for each data type.
In any event, here's my code and the error message:
Code:
#include <iostream>
#include <string>
using namespace std;
template <class Type, int MAXSIZE>
class Array {
private:
int mSize; // capacity of array
Type data[MAXSIZE];
public:
Array(); // create an empty array
Array(char *);
~Array();
int num() const; // number of elements in array
Type &operator[](int i); // array indexing, for assignment
Type operator +(const Array<Type, MAXSIZE> &a)
{
Array<Type,MAXSIZE> y;
for(int i = 0; i < mSize; i++)
y[i] = data[i]+a[i];
return y;
}
Type operator -(const Array<Type, MAXSIZE> &a)
{
Array<Type,MAXSIZE> y;
for(int i =0; i < mSize; i++)
y[i] = data[i]-a[i];
return y;
}
Type & operator =(Array<Type, MAXSIZE> z){
for(int i = 0; i < cap; i++)
{
data = data[i];
}
return *this;
}
bool operator ==(const Array<Type, MAXSIZE> & ) const;
bool operator > (const Array<Type, MAXSIZE> & ) const;
bool operator < (const Array<Type, MAXSIZE> & ) const;
};
template <class Type, int MAXSIZE>
Array<Type, MAXSIZE>::Array():mSize(0){
}
template <class Type, int MAXSIZE>
Array<Type, MAXSIZE>::~Array(){
}
template <class Type, int MAXSIZE>
int Array<Type, MAXSIZE>::num() const
{
return mSize;
}
template<class Type, int MAXSIZE>
Array<Type,MAXSIZE>::Array(char * stringInput)
{
mSize = strlen(stringInput);
if(strlen(stringInput) >= MAXSIZE)
{
cout << "error. not enough memory set aside for input" << endl;
cout << "enter any key to exit" << endl;
char ch;
cin >> ch;
exit(1);
}
else
strcpy(data, stringInput);
}
template <class Type, int MAXSIZE>
bool Array<Type,MAXSIZE>:: operator ==(const Array<Type, MAXSIZE> &z) const {
if (mSize != z.mSize)
{
return false;
} else {
return true;
}
}
template <class Type, int MAXSIZE>
bool Array<Type, MAXSIZE>:: operator >(const Array<Type, MAXSIZE> &x) const {
return (data > x.data);
}
template <class Type, int MAXSIZE>
bool Array<Type,MAXSIZE>::operator <(const Array<Type, MAXSIZE> &x) const {
return (x.data < data);
}
template <class Type, int MAXSIZE>
Type &Array<Type, MAXSIZE>::operator[](int i) {
return data[i];
}
template <class Type, int MAXSIZE>
ostream &operator<<(ostream &s, const Array<Type, MAXSIZE> &a) { // print array
int i;
for (i=0; i<a.num(); i++)
s << "[" << i << "]=" << a[i] << ", ";
s << "end" << endl;
return s;
}
int main()
{
Array<int,8> test,test2,test3;
int i;
cout << "Test Integer Array output:\n";
for (i=0; i<8; i++)
{
test[i]=i;
cout << test[i];
}
cout << "\nTest2 Integer Array output:\n";
for (i=0; i<8; i++)
{
test2[i]=i;
cout << test2[i];
}
cout << "\nTest3 Integer Array output:\n";
for (i=0; i<5; i++)
{
test3[i]=i;
cout << test3[i];
}
cout << "\n";
if (test == test2)
{
cout << "We have a match!\n";
} else {
cout << "Sorry, no match!\n";
}
if (test3 > test)
{
cout << "Test3 is the bigger array.\n";
} else {
cout << "Test is the bigger array.\n";
}
Array<char,5> chartest="blah";
Array<char,6> chartest2="blah2";
for (i=0; i<5; i++)
{
cout << chartest[i];
}
if (chartest == chartest2)
{
cout << "We have a match between chartest and chartest2! \n";
} else {
cout << "No match between chartest and chartest2!\n";
}
return 0;
}
And the error msg:
Code:
C:\Program Files\Microsoft Visual Studio\MyProjects\test\TArray.cpp(181) : error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'class Array<char,6>' (or there is no acceptable conversion)
Error executing cl.exe.
Other than overloading the already overloaded operator (which seems silly and I don't think would work), I am not sure what else I can do to address this error. Any ideas would be awesome..
Thanks again!!!!!!