ahoj,
mama sablonu tridy, trida ma pratelskou fci friend EArray<T> operator+<>(EArray<T>&, EArray<T>&) ...
kdyz to chci ykompilovat tak mi to napise
EArray.cpp:213: error: declaration of ‘operator+’ as non-function
EArray.cpp:213: error: expected ‘;’ before ‘<’ token
nevite co to sakra je ?? m2lo by to fungovat normalne ale nefunguje. Je tam nekde chybka kterou nemuzu najit.
prosim pomooooocccc ... diky opravdu moc.
prikladam zdrojak (jeste neni dops takze je v nem celkem binec :))
#include <iostream>
using namespace std;
// class for exception
class Unknown
{
public:
Unknown() {};
~Unknown(){};
void PrintError()
{
cout << "Unknown ERROR in class EArray. Called invalid class!";
cout << "Can not continue !" << endl;
}
};
class Size
{ public:
Size(int* sizeofallocation,int* maxallocation)
{
eSSizeOfAllocation = sizeofallocation;
eSMaxAllocation = maxallocation;
}
~Size()
{
}
void PrintValue()
{
cout << "SizeOfAllocation: " << *(this->eSSizeOfAllocation) << endl;
cout << "MaxAllacation: " << *(this->eSMaxAllocation) << endl;
cout << endl;
}
void PrintError()
{
cout << "ERROR size in class EArray: " << endl;
PrintValue();
}
private: // S => Size, e => extension
int* eSSizeOfAllocation; // size Of Allocation .......
int* eSMaxAllocation; // MaxAllocation
};
class Zero: public Size
{
public:
Zero(int* sizeofallocation,int* maxallocation):
Size(sizeofallocation, maxallocation)
{
}
~Zero()
{
}
void PrintError()
{
cout << "ERROR size(Zero) in class EArray: value of SizeOfAllocation is zero" << endl;
PrintValue();
}
};
class Minus: public Size
{
public:
Minus(int* sizeofallocation,int* maxallocation):
Size(sizeofallocation, maxallocation)
{
}
~Minus(){}
void PrintError()
{
cout << "ERROR size(Minus) in class EArray. Some of the values is less than zero" << endl;
PrintValue();
}
};
class Plus: public Size
{
public:
Plus(int* sizeofallocation, int* maxallocation):
Size(sizeofallocation, maxallocation)
{
}
~Plus(){}
void PrintError()
{
cout << "ERROR size(Plus) in class EArray. SizeOfAllocation is bigger than MaxAllocation ()" << endl;
PrintValue();
}
};
class Index: public Size
{
public:
Index(int* sizeofallocation, int* maxallocation, int* index):
Size(sizeofallocation, maxallocation)
{
eSIndex = index;
}
~Index()
{
}
void PrintError()
{
cout << "ERROR size(Index) in class EArray." << endl;
PrintValue();
cout << "Index: " << *(this->eSIndex) << endl;
}
int* eSIndex;
};
class NotIdentical
{
public:
NotIdentical(int* sizeofallocation, int* maxallocation)
{
eNSizeOfAllocation = sizeofallocation;
eNMaxAllocation = maxallocation;
}
~NotIdentical() {}
void PrintValue()
{
cout << "SizeOfAllocation:" << *eNSizeOfAllocation << endl;
cout << "MaxAllocation: " << *eNMaxAllocation << endl;
cout << endl;
}
void PrintError()
{
cout << "Acces to NotIdentical classes" << endl;
PrintValue();
}
int* eNSizeOfAllocation;
int* eNMaxAllocation;
};
// X - operator * --> multiplication
class NotIdenticalX : public NotIdentical
{
public:
NotIdenticalX(int* sizeofallocation, int* maxallocation):
NotIdentical(sizeofallocation, maxallocation)
{
}
~NotIdenticalX() {}
void PrintError()
{
cout << "Error in multiplication !!" << endl;
PrintValue();
}
};
// M - operator (-) --> Minus
class NotIdenticalM: public NotIdentical
{
public:
NotIdenticalM(int* sizeofallocation, int* maxallocation):
NotIdentical(sizeofallocation, maxallocation)
{
}
~NotIdenticalM() {}
void PrintError()
{
cout << "Error in 'Minus' !!!" << endl;
PrintValue();
}
};
// ******start class definiton *********
template <class T>
class EArray
{
public:
EArray(int sizeofallocation, int maxallocation);
EArray(const EArray& orig);
EArray(T &); // Converts T to object EArray
virtual ~EArray();
void SetSizeOfAllocation(int sizeofallocation) { SizeOfAllocation = sizeofallocation;}
void SetMaxAllocation(int maxallocation) { MaxAllocation = maxallocation;}
int GetSizeOfAllocation() const { return SizeOfAllocation;}
int GetMaxAllocation() const { return MaxAllocation;}
T& operator[](int);
T operator[](int) const;
void operator+=(const EArray &); // EArray = EArray
EArray& operator+(const EArray &); //
EArray& operator+(T &);
EArray& operator=(const EArray &);
EArray& operator=(T &);
EArray& operator*(const EArray &);
EArray& operator-(const EArray &);
friend EArray<T>& operator+<> (const EArray<T>& left, const EArray<T>& right);
private:
EArray(int sizeofallocation, int maxallocation, int usedallocation);
EArray();
void CheckError(int index); // Check error, if necessary, throw an exception
T GetDefaultValue();
bool CheckUsedIndex(int&);
int SizeOfAllocation; // size of array allocation
int MaxAllocation; // max size of array
T* NewArray; // array :)
bool Error; // Error :)
static const int DEFAULT_MALLOC = 0; // default mAx Allocation (0 = unlimited)
static const int DEFAULT_SOA = 5; //default Size Of Allocation
static const short int DEFAULT_MINUS = -2;
};
template <class T>
EArray<T>::EArray(const EArray& orig)
{
CheckError();
EArray * help = new EArray(this->UsedAllocation);
int i;
for(i = 0; i < this->UsedAllocation; i++)
{
*(help)[i] = orig[i];
}
} // end function
template <class T>
EArray<T>::EArray():
SizeOfAllocation(DEFAULT_SOA),
MaxAllocation(DEFAULT_MALLOC),
Error(false)
{ // CheckError() is unnecessary to do
NewArray = new T[this->SizeOfAllocation];
int i;
for(i = 0; i < 0; i++)
{
NewArray[i] = GetDefaultValue();
}
} // end function
template <class T>
EArray<T>::EArray(int sizeofallocation,int maxallocation):
Error(false) // other definition in next line :)
{
this->SizeOfAllocation = sizeofallocation;
this->MaxAllocation = maxallocation;
NewArray = new T[this->SizeOfAllocation];
int i;
for(i = 0; i < this->SizeOfAllocation; i++)
{
NewArray[i] = GetDefaultValue();
}
CheckError(DEFAULT_MINUS);
} // end function
template <class T>
EArray<T>::EArray(int sizeofallocation, int maxallocation, int usedallocation):
SizeOfAllocation(sizeofallocation),
MaxAllocation(maxallocation),
Error(false)
{
CheckError(DEFAULT_MINUS);
NewArray = new T[this->SizeOfAllocation];
} // end function
template <class T>
EArray<T>::EArray(T& value):
Error(false)
{
EArray<T>* helpobject = new EArray<T>(1,1,1);
helpobject[0] = value;
*helpobject;
} // end function
template <class T>
void EArray<T>::CheckError( int index)
{
if(this->Error == true)
{
throw Unknown();
}
if( this->SizeOfAllocation < 0 || this->MaxAllocation < 0)
{
this->Error = true;
cout << "minuuusss" << endl;
throw Minus(&SizeOfAllocation, &MaxAllocation);
}
if(this->MaxAllocation != 0)
{
if( (this->SizeOfAllocation - this->MaxAllocation) > 0 )
{
this->Error = true;
cout << "Plus" << endl;
throw Plus(&SizeOfAllocation, &MaxAllocation);
}
if( this->SizeOfAllocation > this->MaxAllocation)
{
this->Error = true;
cout << "index1" << endl;
throw Index(&SizeOfAllocation, &MaxAllocation, &index);
}
if( (index + 1) > this->MaxAllocation)
{
this->Error = true;
cout << "index2" << endl;
throw Index(&SizeOfAllocation, &MaxAllocation, &index);
}
}
} // end function
template <class T>
EArray<T>::~EArray()
{
delete[] this->NewArray;
} // endfunction
template <class T>
T EArray<T>::GetDefaultValue()
{
return (T)0;
}
template <class T>
bool EArray<T>::CheckUsedIndex(int& index)
{
if(NewArray[index] == GetDefaultValue())
{
return false;
} else {
return true;
}
} //end function
// OVERLAP OPERATORS ****************************************
template <class T>
T EArray<T>::operator[](int index) const
{
if( index >= this->SizeOfAllocation)
{
return NewArray[this->SizeOfAllocation - 1];
} else {
return NewArray[index];
}
} // end function
template <class T>
T& EArray<T>::operator[](int index) // EArray[index] = T
{
CheckError(index);
if(index >= this->SizeOfAllocation)
{
int helpsize = 0;
if((this->SizeOfAllocation - this->MaxAllocation) < (DEFAULT_SOA) && this->MaxAllocation != 0)
{
helpsize = this->MaxAllocation - this->SizeOfAllocation;
} else {
helpsize = DEFAULT_SOA;
}
T * helparray = new T[this->SizeOfAllocation + helpsize];
int i;
for(i = 0; i < (this->SizeOfAllocation + helpsize); i++)
{
if( i < this->SizeOfAllocation)
{
helparray[i] = NewArray[i];
}
if( i >= helpsize)
{
helparray[i] = GetDefaultValue();
}
}
delete[] NewArray;
NewArray = helparray;
this->SizeOfAllocation = index + 1;
return NewArray[index];
} else if(index < this->SizeOfAllocation)
{
return NewArray[index]; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!§§§
}
} // end function
template <class T>
EArray<T> & EArray<T>::operator=(const EArray & right)
{
CheckError(DEFAULT_MINUS);
this->MaxAllocation = right.GetMaxAllocation();
this->SizeOfAllocation = right.GetSizeOfAllocation();
int i;
for(i = 0; i < right.SizeOfAllocation; i++)
{
NewArray[i] = right[i];
}
return *this;
} // end function
template <class T>
EArray<T>& EArray<T>::operator=(T& right)
{
CheckError(DEFAULT_MINUS);
this->MaxAllocation = right.GetMaxAllocation();
this->SizeOfAllocation = right.GetSizeOfAllocation();
} // end funcion
template <class T>
EArray<T> & EArray<T>::operator+(const EArray & right) // EArray + (EArray)right
{
CheckError(DEFAULT_MINUS);
int helpSizeOfAllocation = this->SizeOfAllocation + right.GetSizeOfAllocation();
int helpMaxAllocation = this->MaxAllocation + right.GetSizeOfAllocation();
EArray<T>* helpobject = new EArray(helpSizeOfAllocation, helpMaxAllocation);
int i;
for(i = 0; i < helpSizeOfAllocation; i++)
{
if( i <= helpSizeOfAllocation)
{
helpobject[i] = NewArray[i];
}
if( i > helpSizeOfAllocation)
{
helpobject[i] = right[i];
}
}
return helpobject;
} // end function
template <class T>
EArray<T>& EArray<T>::operator+(T& right) // EArray + (T)right;
{
CheckError(DEFAULT_MINUS);
EArray<T>* helpobject = new EArray<T>(this->SizeOfAllocation, this->MaxAllocation);
int i;
for(i = 0; i < this->SizeOfAllocation; i++)
{
(*helpobject)[i] = NewArray[i];
}
return *helpobject;
} // end function;
template <class T>
void EArray<T>::operator+=(const EArray& right) // EArray += (EArray)right
{
CheckError(DEFAULT_MINUS);
if(right.GetMaxAllocation() == 0)
{
this->MaxAllocation = 0;
} else {
this->MaxAllocation += right.GetMaxAllocation(); // help Total Used Alocacion ...
}
this->SizeOfAllocation += right.GetSizeOfAllocation();
T* helparray = new T[this->SizeOfAllocation + right.GetSizeOfAllocation()];
int i;
for(i = 0; i < (this->SizeOfAllocation + right.GetSizeOfAllocation()); i++)
{
if(i < this->SizeOfAllocation)
{
helparray[i] = NewArray[i];
}
if(i >= this->SizeOfAllocation)
{
helparray[i] = right[i];
}
}
delete[] NewArray;
NewArray = helparray;
} // end function
template <class T>
EArray<T>& EArray<T>::operator*(const EArray& right) // EArray = EArray*(EArray)right;
{
CheckError(DEFAULT_MINUS);
if(SizeOfAllocation = right.GetSizeOfAllocation())
{
EArray* helpobject = new EArray(this->SizeOfAllocation, right.GetMaxAllocation());
int i;
for(i = 0; i < this->SizeOfAllocation; i++)
{
(*helpobject)[i] = NewArray[i]*right[i];
}
return *helpobject;
} else {
this->Error = true;
throw NotIdenticalX(this->SizeOFAllocation, this->MaxAllocation);
}
} // end function
template <class T>
EArray<T>& EArray<T>::operator-(const EArray& right) // EArray = EArray - (EArray)right
{
CheckError(DEFAULT_MINUS);
if(this->SizeOfAllocation == right.GetSizeOfAllocation())
{
EArray<T>* helpobject = new EArray<T>(this->SizeOfAllocation, this->MaxAllocation);
int i;
for(i = 0; i < this->SizeOfAllocation; i++)
{
(*helpobject)[i] = NewArray[i] - right[i];
}
return *helpobject;
} else {
this->Error = true;
throw NotIdenticalM(&(this->SizeOfAllocation), &(this->MaxAllocation));
}
} // end function
/**********************************************************************************************************************************/
/************** friend FUNCTIONS***************************************************************************************************/
/***********************************************************************************************************************************/
template <class T>
EArray<T>& operator+(const EArray<T>& left, const EArray<T>& right)
{
EArray<T>* helpobject = new EArray<T>(left.GetSizeOfAllocation(),
right.GetMaxAllocation());
*helpobject = left + right;
return *helpobject;
}
/*
template <class T>
EArray<T>& operator-
*/
int main()
{
int h = 5;
EArray<int> help(10, 0);
EArray<int> help2(10, 0);
EArray<int>* help3 = new EArray<int>(10, 56);
help[0] = h;
help2[0] = h;
(*help3)[2] = 56;
cout << "help, SizeOfAllocation: " << help.GetSizeOfAllocation() << endl;
cout << "help2, Sizeofall : " << help2.GetSizeOfAllocation() << endl;
help2 += help;
*help3 = help - help2;
cout << "constrol pocet :" << help2.GetSizeOfAllocation() << endl;
cout << endl << (*help3)[0] << endl;
*help3 = help2 + h;
cin >> h;
}