gpt4 book ai didi

c++ - 错误 C4716 'Party::operator+' : must return a value

转载 作者:行者123 更新时间:2023-11-28 04:07:19 24 4
gpt4 key购买 nike

Party Party::operator + (const Party& party2)
{
Party newParty;
newParty.maxAttendees = maxAttendees + party2.maxAttendees;
newParty.numAttendees = numAttendees + party2.numAttendees;

for (int c = 0; c < getNumAttendees(); c++)
newParty.attendees[c] = attendees[c];

for (int c = numAttendees, d = 0; c < party2.numAttendees; c++, d++)
newParty.attendees[c] = party2.attendees[d];

if (date.compare(party2.date) == 0)
newParty.date = date;

if (location.compare(party2.location) == 0)
newParty.location = location;

if (organizer.compare(party2.organizer) == 0)
newParty.organizer = organizer;


}

它说 Party::operator+ 必须返回一个值。我尝试返回 party2newParty 等,但出现更多错误。

最佳答案

这个错误就是它在 jar 上写的;由于您声明它返回一个 Party,因此您需要在函数末尾返回一个值:

Party Party::operator + (const Party& party2)
{
// Some implementation...

return newParty;
}

无论如何不返回一个值是没有意义的,因为加法通常会产生某种新值。


注意:您可能还需要考虑将函数设置为 const,因为您的参数是 const:

Party Party::operator + (const Party& party2) const
{
//...
}

关于c++ - 错误 C4716 'Party::operator+' : must return a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495553/

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