【问题标题】:how to write method definition for the Product* getProductFromID(std::string);如何为 Product* getProductFromID(std::string) 编写方法定义;
【发布时间】:2017-01-30 08:43:36
【问题描述】:

请告诉我如何为函数编写定义:

Product* getProductFromID(std::string);

void Store:: addMember(Customer* c)

addmember shud 将成员详细信息添加到名为 cart 的向量中 我已经尝试过这样的事情

void Store:: addMember(Customer* c)
{
    Customer c(std::string n, std::string a, bool pm);
    members.push_back(n.str());
}

我收到一条错误消息,提示 [Error] 'n' was not declared in this scope

【问题讨论】:

  • 您将变量声明为Customer c 的构造函数的参数。应该在外面声明它们,并为良好的度量赋予一个初始值。
  • @Jonas 如果你只是在方括号中输入 MCVE,你会得到一个格式很好的链接:minimal reproducible example
  • @BoBTFish,哇,我自己把它格式化为一个普通的链接,天知道多久了...[MCVE](link)...
  • @Jonas 谢谢我刚刚做到了

标签: c++ class methods definitions


【解决方案1】:

这一行

Customer c(std::string n, std::string a, bool pm);

声明一个函数c,它接受三个参数并返回一个Customer。根本不是你想要的。

假设 Customer 包含一个 n 成员(确实需要一个更具描述性的名称),该函数看起来就像

void Store:: addMember(Customer* c)
{
    members.push_back(c->n.str());
}

【讨论】:

  • 我照你说的做了,谢谢你现在建议代码是这种方式void Store:: addMember(Customer* c) { members.push_back(c->name.str()); },我收到一个错误,说 [Error] 'std::string Customer::name' is private跨度>
  • 我得到了一个函数,通过它我可以访问变量thankyou @Bo Persson
猜你喜欢
  • 1970-01-01
  • 2014-05-21
  • 2023-03-21
  • 2023-03-04
  • 1970-01-01
  • 2015-07-20
  • 2023-04-03
  • 2011-02-27
  • 1970-01-01
相关资源
最近更新 更多