Dave
Dave

Reputation: 141

template function error: no operator found which takes a right hand operand of type

Hey! check out the following example.. its almost a copy paste from some cpp book. i can't understand why is doesn't compile (under windows). it says:

'<<' : no operator found which takes a right-hand operand of type 'div_t' (or there is no acceptable conversion)  

this is the example:

#include <iostream>

template <class T>
T div(T a, T b) {
    T result =  a/b;
    return result;
}

int main() {    
    int a = 5;
    int b = 3;
    std::cout << "INT " << div(a,b) << std::endl; //this line output the error

    return 0;
}

thanks!!

Upvotes: 1

Views: 843

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320421

div is a standard library function that returns a value of div_t type. When you included <iostream>, you apparently also indirectly included the declaration of the standard div. This is what the compiler is trying to use, not your template version.

This is probably the fault of the implementation, not your fault (assuming that the code you posted is the exact code you are trying to compile). If they include that portion of standard library in <iostream>, they probably should have done it in such a way that standard div would have become std::div. If they did it that way, you wouldn't have this problem.

You can do

std::cout << "INT " << div<>(a,b) << std::endl;

to explicitly ask the compiler to use your template.

Upvotes: 5

Related Questions