最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Error C2280 occurs in class template function in Visual Studio 2022 - Stack Overflow

programmeradmin2浏览0评论

Error C2280 occurs in class template function in Visual Studio 2022 The compiler reports that the function is deleted because a data member of type Reference is used

Classes containing reference type data members can be compiled and executed normally

class CReference {
public:
        double&         m_Reference;
                        CReference(double& Data) :
                            m_Reference(Data) 
                        {                                
                        }
virtual                 ~CReference() {};
                        CReference(const CReference& Obj) :
                            m_Reference(Obj.m_Reference) {
                        }
        CReference&     operator = (const CReference& Obj) {
                            m_Reference = Obj.m_Reference;
                            return *this;
                        }
};
int main() {
    double e0 = 1.0, e1 = 2.0;
    CReference R0(e0);
    CReference R1(e1);
    CReference R2(R0);
    R2 = R1;
}

Template classes and template functions can also be compiled and executed normally

template <typename Type>
class CVectorReference2 {
public:
         Type& m_x, & m_y;                                      
         Type& m_s, & m_t;                                      
         CVectorReference2(Type& Value0, Type& Value1) :
            m_x(Value0), m_y(Value1),
            m_s(Value0), m_t(Value1)    {    }
virtual ~CVectorReference2() {}                              
         CVectorReference2(const CVectorReference2<Type>& Vector) :
            m_x(Vector.m_x), m_y(Vector.m_y),
            m_s(Vector.m_s), m_t(Vector.m_t)    {    }
         CVectorReference2<Type>& operator=(const CVectorReference2<Type>& Vector) {        
             Assign(Vector);       
             return *this;
         }
         template<typename InputType>
         inline void Assign(const CVectorReference2<InputType>& Vector)     {
             m_x = (Type)Vector.m_x;
             m_y = (Type)Vector.m_y;
         }
};
int main() {
    double e0, e1, e2 = 1.0, e3 = 2.0;
    CVectorReference2<double> P0(e0, e1);
    CVectorReference2<double> P1(e2, e3);
    CVectorReference2<double> P2(P0);
    P2 = P1;
}

But when operator= is replaced with template function, Error C2280 is reported saying that reference type data members are used? why?

This error message is really strange, how can I troubleshoot it?

template <typename Type>
class CVectorReference2 {
public:
             Type& m_x, & m_y;                                      
             Type& m_s, & m_t;  
             CVectorReference2(Type& Value0, Type& Value1) :
                 m_x(Value0), m_y(Value1),
                 m_s(Value0), m_t(Value1)    {    }
virtual     ~CVectorReference2() {}                              
             CVectorReference2(const CVectorReference2<Type>& Vector) :
                 m_x(Vector.m_x), m_y(Vector.m_y),
                 m_s(Vector.m_s), m_t(Vector.m_t)    {    }
             template<typename InputType>  //<-------
             CVectorReference2<Type>& operator=(const CVectorReference2<InputType>& Vector) {
                 Assign(Vector);       
                 return *this;
             }
             template<typename InputType>
             inline void Assign(const CVectorReference2<InputType>& Vector)     {
                 m_x = (Type)Vector.m_x;
                 m_y = (Type)Vector.m_y;
             }
};
int main() {
    double e0, e1, e2 = 1.0, e3 = 2.0;
    CVectorReference2<double> P0(e0, e1);
    CVectorReference2<double> P1(e2, e3);
    CVectorReference2<double> P2(P0);
    P2 = P1;
}

Error C2280 occurs in class template function in Visual Studio 2022 The compiler reports that the function is deleted because a data member of type Reference is used

Classes containing reference type data members can be compiled and executed normally

class CReference {
public:
        double&         m_Reference;
                        CReference(double& Data) :
                            m_Reference(Data) 
                        {                                
                        }
virtual                 ~CReference() {};
                        CReference(const CReference& Obj) :
                            m_Reference(Obj.m_Reference) {
                        }
        CReference&     operator = (const CReference& Obj) {
                            m_Reference = Obj.m_Reference;
                            return *this;
                        }
};
int main() {
    double e0 = 1.0, e1 = 2.0;
    CReference R0(e0);
    CReference R1(e1);
    CReference R2(R0);
    R2 = R1;
}

Template classes and template functions can also be compiled and executed normally

template <typename Type>
class CVectorReference2 {
public:
         Type& m_x, & m_y;                                      
         Type& m_s, & m_t;                                      
         CVectorReference2(Type& Value0, Type& Value1) :
            m_x(Value0), m_y(Value1),
            m_s(Value0), m_t(Value1)    {    }
virtual ~CVectorReference2() {}                              
         CVectorReference2(const CVectorReference2<Type>& Vector) :
            m_x(Vector.m_x), m_y(Vector.m_y),
            m_s(Vector.m_s), m_t(Vector.m_t)    {    }
         CVectorReference2<Type>& operator=(const CVectorReference2<Type>& Vector) {        
             Assign(Vector);       
             return *this;
         }
         template<typename InputType>
         inline void Assign(const CVectorReference2<InputType>& Vector)     {
             m_x = (Type)Vector.m_x;
             m_y = (Type)Vector.m_y;
         }
};
int main() {
    double e0, e1, e2 = 1.0, e3 = 2.0;
    CVectorReference2<double> P0(e0, e1);
    CVectorReference2<double> P1(e2, e3);
    CVectorReference2<double> P2(P0);
    P2 = P1;
}

But when operator= is replaced with template function, Error C2280 is reported saying that reference type data members are used? why?

This error message is really strange, how can I troubleshoot it?

template <typename Type>
class CVectorReference2 {
public:
             Type& m_x, & m_y;                                      
             Type& m_s, & m_t;  
             CVectorReference2(Type& Value0, Type& Value1) :
                 m_x(Value0), m_y(Value1),
                 m_s(Value0), m_t(Value1)    {    }
virtual     ~CVectorReference2() {}                              
             CVectorReference2(const CVectorReference2<Type>& Vector) :
                 m_x(Vector.m_x), m_y(Vector.m_y),
                 m_s(Vector.m_s), m_t(Vector.m_t)    {    }
             template<typename InputType>  //<-------
             CVectorReference2<Type>& operator=(const CVectorReference2<InputType>& Vector) {
                 Assign(Vector);       
                 return *this;
             }
             template<typename InputType>
             inline void Assign(const CVectorReference2<InputType>& Vector)     {
                 m_x = (Type)Vector.m_x;
                 m_y = (Type)Vector.m_y;
             }
};
int main() {
    double e0, e1, e2 = 1.0, e3 = 2.0;
    CVectorReference2<double> P0(e0, e1);
    CVectorReference2<double> P1(e2, e3);
    CVectorReference2<double> P2(P0);
    P2 = P1;
}
Share Improve this question asked Mar 10 at 13:55 CROMACROMA 213 bronze badges 3
  • The templated version of operator= (in the last snippet) is not really a proper assignment operator, which is therefore by default deleted (due to the reference member). This is different in the 2nd and 1st snippets where you actually supply a proper assignment operator which is therefore no longer deleted. – wohlstad Commented Mar 10 at 14:03
  • Don't do this, references are not re-assignable for a reason. And having member variables that are references will cause your move and copy constructors to be invalid (and the default ones deleted) – Pepijn Kramer Commented Mar 10 at 14:33
  • I don't know what's wrong with this grammatically? Does the C++ compiler have any problems with the secondary expansion of template functions? – CROMA Commented Mar 10 at 15:01
Add a comment  | 

1 Answer 1

Reset to default 2

The special member functions of the forms: copy constructor, copy assignment, move constructor, move assignment, and destructor are never function templates.

There is an implicit CVectorReference2<Type>& operator=(const CVectorReference2<Type>& Vector) that is deleted because you have a reference member. You need to add that function like you did in code block number 2 so that the assignment operator is not deleted.

发布评论

评论列表(0)

  1. 暂无评论