Home Implicit conversion not working in template class
Reply: 0

Implicit conversion not working in template class

user1139 Published in May 21, 2018, 3:05 am

I have the following class and when I run test code to test operator/, the compiler tells me the following (referring to the comparison made in the divide operator function)

C2678: binary '==': no operator found which takes a left-hand operand of type 'const foo<8>' (or there is no acceptable conversion)

template<unsigned int t>
class foo {
  foo(int i) {
    // implicit conversion

  template<unsigned int u>
  bool operator==(const foo<u>& obj) const {
    // do some comparison here

  template<unsigned int u>
  foo operator/(const foo<u>& divisor) const {
    if (divisor == 0)
    // perform division
    // return foo result

However, when I add the following code into the class, there's no error:

friend bool operator==(const foo<t>& lhs, const foo<t>& rhs) {
  // do some comparison here

My test code looks like this:

int main() {
  foo<8> foo_a(0);
  foo<4> foo_b(123);
  foo_b / foo_a;
  return 0;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317038 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO