I'm doing my own array template version of an array.
I'm having some problems overloading a method.
I'm doing a specifik version of a "print()" if the type of array is <char>,(i'm saving bools as char).
this works without problems. I just needed to add a templete<> infront of my scpeciales "print()". This is shown at line 32-47.
Now I need to do an array version of division.
The general case is an array of floats, this will work without problem
So given normal Array with floats, my general version line 51-62 works.
But if I use a array<int>, i don't want it to return a array<int>, it should return a array<float>. (like float division)
But I'm having problems with the defintion.
Can someone give me a solution, thanks.
#include <iostream>
template<typename T>
class Array {
public:
Array() {puts("empty constructor");data_=NULL;x_=0;}
Array(int length):x_(length),data_(new T[length]){}
void init(int length) {x_=length;data_=new T[length]; }
Array(const Array<T>& var);
~Array(){delete [] data_;}
int length() const { return x_; }
T& operator() (uint r);
T operator() (uint r) const;
void print(int x=0, char sep=' ');
void fillUp(T var) {for(int i=0;i<x_;i++) data_[i]=var;}
void plus(const Array<T>&);
Array<char> operator< (const float &f);
Array<char> operator> (const float &f);
Array<char> operator== (const float &f);
int numTrues() const {return numOnes_;}
Array<T>& operator= (const Array<T>&);
Array<T> operator+ (const Array<T>&);
Array<T> operator/ (const float &other);
private:
int x_;
T* data_;
int numOnes_;
};
template<typename T>
void Array<T>::print(int x,char sep){
printf("printing array with dim=%d\n",x_);
for(int i=0;i<x_;i++)
std::cout << data_[i] << sep;
std::cout <<endl;
}
template<>
void Array<char>::print(int x,char sep){
printf("printing array with dim=%d\n",x_);
for(int i=0;i<x_;i++)
std::cout <<(int) data_[i] << sep;
std::cout <<endl;
}
template<class T>//general
Array<T> Array<T>::operator/ ( float const & other ){
if(other==0){
puts("ohh my god, divide by zero thats stupid, will exit");
exit(0);
}
Array<T> tmp = Array<T>(x_);
for(int i=0;i<x_;i++)
tmp.data_[i] = data_[i] / other;
return tmp;
}
template<>//int specs
Array<float> Array<int>::operator/ ( float const & other ){
if(other==0){
puts("ohh my god, divide by zero thats stupid, will exit");
exit(0);
}
Array<float> tmp = Array<float>(x_);
for(int i=0;i<x_;i++)
tmp.data_[i] = (float))data_[i] / other;
return tmp;
}