简体   繁体   English

模板化运算符重载无法绑定非常量左值引用

[英]templated operator overloading cannot bind non-const lvalue reference

I have tried variations of const and passing by reference, but seem to have problems down every corridor.我尝试过 const 和通过引用传递的变体,但似乎在每个走廊上都有问题。 This configuration gives the error cannot bind non-const lvalue reference of type 'thingArr&' to an rvalue of type 'thingArr' what am I doing incorrectly?此配置导致错误无法将“thingArr&”类型的非常量左值引用绑定到“thingArr”类型的右值我做错了什么?

#include <iostream>
using namespace std;
template <typename T>
class thingArr
{
public:
  thingArr(){for(int i=0;i<4;++i)n[i] = 1;}
  thingArr(thingArr& mIn){for(int i=0; i<4; ++i)n[i]=mIn.n[i];}
  friend std::ostream& operator << (std::ostream& s, thingArr<T>& m)
  {
    s<<"\n("<<m.n[0]<<", "<<m.n[1]<<", "<<m.n[2]<<", "<<m.n[3]<<")";
    return s;
  }
  friend thingArr operator * (thingArr inThingArr, T inScalar)
  {
    thingArr out(inThingArr);
    for(int i=0;i<4;++i)out.n[i]*=inScalar;
    return thingArr(out);
  }

  T n[4];
};

main(){
  thingArr<float> A;
  thingArr<float> B;
  B = A * .25;
  cout <<"A: "<<A<<endl;
  cout <<"B: "<<B<<endl;
}

thanks to SM, I have a working version:感谢 SM,我有一个工作版本:

#include <iostream>
using namespace std;
template <typename T>
class thingArr
{
public:
  thingArr(){for(int i=0;i<4;++i)n[i] = 1;}
  thingArr(const thingArr& mIn){for(int i=0; i<4; ++i)n[i]=mIn.n[i];}
  friend std::ostream& operator << (std::ostream& s, thingArr<T>& m)
  {
    s<<"\n("<<m.n[0]<<", "<<m.n[1]<<", "<<m.n[2]<<", "<<m.n[3]<<")";
    return s;
  }
  friend thingArr operator * (const thingArr& inThingArr, T inScalar)
  {
    thingArr out(inThingArr);
    for(int i=0;i<4;++i)out.n[i]*=inScalar;
    return thingArr(out);
  }

  T n[4];
};

main(){
  thingArr<float> A;
  thingArr<float> B;
  B = A * .25;
  cout <<"A: "<<A<<endl;
  cout <<"B: "<<B<<endl;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型的非常量左值引用绑定到类型的右值 - cannot bind non-const lvalue reference of type to an rvalue of type 类型的非常量左值引用无法绑定错误 - non-const lvalue reference to type cannot bind error 类型“”的非常量左值引用不能绑定到类型“ *”的临时对象 - non-const lvalue reference to type '' cannot bind to a temporary of type ' *' 将非常量左值引用绑定到右值 - Bind non-const lvalue reference to rvalue 对指针错误的引用:非常量左值引用“ const * FooBarClass”无法绑定到临时对象 - Reference to a pointer error: Non-const lvalue reference “const * FooBarClass” cannot bind to a temporary 难以理解 c++ 中的 const(无法绑定非 const 左值引用) - Troubles understanding const in c++ (cannot bind non-const lvalue reference) 对类型“const int *”的非 const 左值引用不能绑定到不相关类型“int *”的值 - non-const lvalue reference to type 'const int *' cannot bind to a value of unrelated type 'int * “对类型的非const左值引用不能绑定”带引用的错误(Type&)但不带有指针(Type *) - “non-const lvalue reference to type cannot bind” error with reference (Type &) but not with pointer (Type *) 如何解决这个问题?非常量引用必须绑定到左值 - How to solve this?non-const reference must bind to lvalue 当从函数调用传递值时,std :: tie失败,并且“无法绑定非常量左值引用” - std::tie fails with “cannot bind non-const lvalue reference” when passed value from a function call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM