c++ - no operator found which takes a left operand... (using a class template) -
i'm overloading operators of class template , error: error 1 error c2678: binary '>' : no operator found takes left-hand operand of type 'const cset < int >'
cset.h:
bool cset<t>::operator>(const cset<t>& myset) const{ bool flag = false; (int = 0; < size; i++){ (int j = 0; j < myset.size; j++){ if (arr[i] == myset[j]){ flag = true; break; } if (j == myset.size - 1 && flag == false) return false; } } return true; } void cset<t>::operator+=(t& myval){ t* temp = new t[size + 1]; (int = 0; < size; i++){ if (arr[i] == myval){ delete[] temp; return; } } (int = 0; < size; i++) temp[i] = arr[i]; delete[] arr; arr = temp; arr[size] = myval; } mian.cpp:
#include "set.h" int main(){ cset<int> myset, yourset; myset += 3; myset += 4; myset += 5; yourset += 5; yourset += 4; yourset += 3; bool boom; boom = myset == yourset; } now, error understand compiler cannot convert t int. question is: why not? isn't whole purpose of templates?
there must that's i'm missing because doesn't make sense (at least me).
you want/need change to:
bool cset<t>::operator>(const cset<t>& myset) const { // ... ^^^^^ note addition here. when invoke member function like:
if (x > y) ... it's equivalent to: if (x.operator>(y)). const on parameter type means right operand can const. allow left operand const well, add const it's shown above.
Comments
Post a Comment