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.

3
  • You wrote a non-defaulted constructor, so no compiler provided operator= is generated Commented May 31, 2017 at 4:49
  • 4
    @PasserBy, That's not true. Constructor has nothing to do with operator= Commented May 31, 2017 at 4:50
  • @Ajay Oh, oops, that's a hole in my memory fixed Commented May 31, 2017 at 7:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
Reference is not pointer. An int* can point to something else later, but an int& cannot reference to something else later.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.