gpt4 book ai didi

c++ - C++ EXC_BAD_INSTRUCTION?

转载 作者:行者123 更新时间:2023-12-02 09:53:12 24 4
gpt4 key购买 nike

写下该类怎么了,它返回以下错误:

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
调用operator =时。
class test
{
int x;
public:
test()
{
this->x=10;
}

test(const test& other)
{
this->x=other.x;
}

test& operator=(const test& other)
{
this->x=other.x;
}

};

最佳答案

您不是从operator=返回。这会引发未定义的行为。

test& operator=(const test& other)
{
this->x=other.x;
}
您需要做:
test& operator=(const test& other)
{
this->x=other.x;
return *this;
}

关于c++ - C++ EXC_BAD_INSTRUCTION?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62626198/

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