gpt4 book ai didi

c++ - 如何编写此方法的定义我收到错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:02 26 4
gpt4 key购买 nike

提前致谢我正在尝试编写方法的定义

Customer* getMemberFromID(std::string);

这是我写的定义,但我收到一些错误消息,说 [Error] 'Customer' in 'class Store' does not name a type

Store:: Customer* getMemberFromID(std::string id)
{
for(int i = 0; i < members.size(); i++)
{
Customer* c = members.at(i);
if(c->getAccountID() == id)
{

return c;
}
}
return NULL;
}

最佳答案

尝试

Customer* Store::getMemberFromId(std::string const& id)

很难说完整Minimal, Complete, and Verifiable example (你真的应该提供),但我猜 getMemberFromIdStore 的成员函数,但是 Customer 不是Store 的成员,您只是对命名规则感到困惑。

请注意,我将 (std::string id) 换成了 (std::string const& id) 以避免字符串的额外拷贝,当你没有需要它。

那么你可以只使用标准算法 find_if :

Customer* Store::getMemberFromId(std::string const& id)
{
auto foundPos = std::find_if(members.begin(),
members.end(),
[&](Customer* c){ return c->getAccountId() == id; });
return (foundPos==members.end())
? nullptr
: *foundPos;
}

关于c++ - 如何编写此方法的定义我收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41934350/

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