0

My code,

template<typename T>
class NamedObject{
public:

    NamedObject(std::string& name, const T& value):nameValue(name), objectValue(value)
    {

    }

private:
    std::string& nameValue;
    const T objectValue;
};

int main(int argc, char* argv[])
{
    NamedObject<int> obj1(std::string("Obj1"),3);
    NamedObject<int> obj2(std::string("Obj2"),3);

    obj2 = obj1; //this line gives error


    return 0;
}

I am getting error,

Error 1 error C2582: 'operator =' function is unavailable in 'NamedObject' c:\users\pkothari\documents\visual studio 2008\projects\stackoflw\stackoflw\stackoflw.cpp 39

I have not provide any operator =, shouldn't compiler provide default one?

@Edit for shown as duplicate: I agree with const, reference can refer to member of another object.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137

1 Answers1

2

Template has nothing to do with it. Your class has a const data member, and has a reference to string. You'd need to remove const attribute has well as reference specifier from the string data member. I would suggest you to implement the class without using templates.

Ajay
  • 18,086
  • 12
  • 59
  • 105
  • I know it has nothing to do with Template. I tried removing const as well. But reference can refer to member of another object, right? – Pranit Kothari May 31 '17 at 05:08
  • Reference is not pointer. An int* can point to something else later, but an int& cannot reference to something else later. – Ajay May 31 '17 at 05:27