gpt4 book ai didi

C++ : use of deleted function

转载 作者:行者123 更新时间:2023-11-28 01:43:14 25 4
gpt4 key购买 nike

我正在绕圈子试图解决这个问题,但仍然没有成功。我确信我混淆了基本的 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& 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 类提供一个构造函数(如果需要,甚至是默认值)并更改您的 B 类构造函数。请记住,类属性和函数默认是私有(private)的

完整答案:

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);
}

关于C++ : use of deleted function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46345686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com