C++ C++;:删除函数的使用
我正在绕圈子试图解决这个问题,但仍然没有运气。我确信我混淆了C++基础的东西,所以需要你的帮助。C++ C++;:删除函数的使用,c++,C++,我正在绕圈子试图解决这个问题,但仍然没有运气。我确信我混淆了C++基础的东西,所以需要你的帮助。 class TypeA { TypeA( const int id ) ; TypeA() ; private : int n_id ; } 然后在我的B班**.h** class TypeB : TypeB( const int x , const int y ) ; TypeB( const int x , const int y , const TypeA&
class TypeA {
TypeA( const int id ) ;
TypeA() ;
private :
int n_id ;
}
然后在我的B班**.h**
class TypeB :
TypeB( const int x , const int y ) ;
TypeB( const int x , const int y , const TypeA& a) ;
private :
int _x ;
int _y ;
TypeA _a ;
我的第二个构造函数有问题
.cpp
TypeB( const int x , const int y , const TypeA& a) : _x( x) , _y(y) {
_a = a ;
}
我得到了这个错误:
use of deleted function TypeA::operator=(TypeA&)
note : TypeA::operator=(TypeA&) is implicity deleted because the default definition would be ill-formed
class TypeA
你知道为什么会这样吗
编辑:
我试过这个:
TypeB( const int x , const int y , const TypeA& a) : _x( x) , _y(y) , _a(a) { }
现在错误变成:
use of deleted function TypeA& _a(a)
note : TypeA is implicity deleted because the default definition would be ill-formed.
class TypeA
这是否意味着问题出在我的typeA的默认构造函数上?给你的typeA类构造函数(即使是你想要的默认构造函数)并更改你的TypeB构造函数。请记住,类属性和函数在默认情况下是私有的 完整答案:
class TypeA {
public :
TypeA() = default;
~TypeA() = default;
private :
int n_id ;
};
class TypeB {
public :
TypeB(const int x ,const int y);
TypeB(const int x ,const int y ,const TypeA& a);
~TypeB() = default;
private :
int _x;
int _y;
TypeA _a;
};
TypeB::TypeB(const int x ,const int y ,const TypeA& a) : _x( x) , _y(y), _a(a) {
}
int main(void)
{
TypeA test;
TypeB hello(10, 10, test);
}
给你的TypeA类一个构造函数(如果你愿意的话,甚至是默认的),然后改变你的TypeB构造函数。请记住,类属性和函数在默认情况下是私有的 完整答案:
class TypeA {
public :
TypeA() = default;
~TypeA() = default;
private :
int n_id ;
};
class TypeB {
public :
TypeB(const int x ,const int y);
TypeB(const int x ,const int y ,const TypeA& a);
~TypeB() = default;
private :
int _x;
int _y;
TypeA _a;
};
TypeB::TypeB(const int x ,const int y ,const TypeA& a) : _x( x) , _y(y), _a(a) {
}
int main(void)
{
TypeA test;
TypeB hello(10, 10, test);
}
你必须定义“public”你的构造函数!!!如果不编写“public”,默认情况下,类中的所有内容都是私有的:
class TypeA {
public: // <--- you need this!!!
TypeA( const int id ) ;
TypeA() ;
private :
int n_id ;
};
A类{
public://您必须定义“public”构造函数!!!如果您不编写“public”,默认情况下,类中的所有内容都是私有的:
class TypeA {
public: // <--- you need this!!!
TypeA( const int id ) ;
TypeA() ;
private :
int n_id ;
};
A类{
public://提示:您没有将赋值用于\ux
或\uy
,而是将其用于\ua
。您的构造函数是私有的,这是有意的吗?无论如何,请提供一个。通过将public:
添加到它应该出现的位置。@TripeHound缺失会导致链接器错误,而不是隐式删除一个赋值运算符提示:您没有为\ux
或\uy
使用赋值,而是为\ua
使用赋值。您的构造函数是私有的,这是有意的吗?无论如何,请提供一个。通过添加public:
它应该出现的位置。@TripeHound缺失会导致链接器错误,而不是隐式删除当您看到const时,您知道它在function@rak007为什么调用方应该关心被调用方是否修改了函数中int的自己副本?这是一个简单的细节。虽然我倾向于省略它,但我可以理解为什么有人会将它保留在函数中。当你见康斯特,你知道它在未来不会改变function@rak007为什么调用者要关心被调用者是否修改了函数中int的自己副本?这是一个简单的细节。虽然我倾向于省略它,但我可以理解为什么有人会将它保留在函数中。