Шаблон функции и перегрузка оператора > в с++
Нужно создать шаблон функции для подсчета элементов, которые больше за значение val. Возникла проблема при перегрузки оператора >. Сравниваем значения age из класса Istota и значение val(вводится пользователем). Компилятор выдает ошибку
//Invalid operands to binary expression ('Istota*' and 'float')//
Подскажите пожалуйста, как исправить эту ошибку? Ниже оставляю фрагменты кода с классом и шаблоном функции.
class Istota: public Forma {
public:
float age, x, y;
int oznaka;
Istota(): oznaka(1){
age=0;}
Istota (float age_ , float x1, float y1):Forma(x1,y1), oznaka(1){
age=age_;
x=x1;
y=y1;}
float getx(){
return x;}
Istota& setx(float x1){
x=x1;
return *this;}
float gety(){
return y;}
Istota& sety( float y1){
y=y1;
return *this;}
float getage(){
return age;}
Istota& setage(float age_){
age=age_;
return *this;}
Istota (const Istota &other){
this->age=other.age;
this->x=other.x;
this->y=other.y;}
~Istota(){}
float val;
bool operator >(const Istota &other){
return (other.age>val);}
};
template <class T>
T* count(T a[],float val, int s){
int sum=0;
for (int i=0;i<s; i++){
if(a[i]>val){ sum ++;} //Invalid operands to binary expression ('Istota *' and 'float')
}
cout<<"Результат:"<<sum<<endl;
return a;
}
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Выбросьте float val; из класса - оно вам не нужно.
Оператор перепишите
bool operator >(float val) const
{
return (age > val);
}
В функции замените
if(a[i]>val){ sum ++;}
на
if( (*a[i])>val ){ sum ++;}
Должно заработать.
Но лучше бы вам функцию поменять... Как-то передавать массив указателей мне кажется не самым красивым...