【问题标题】:How to return a struct from a function如何从函数返回结构
【发布时间】:2020-03-03 06:30:34
【问题描述】:

我正在学习 c++,我的项目是关于队列的。

我有:

struct dough {
    string code;
    int stirringTime;
};

通常在我的任务中,我们使用int 而不是struct。这就是为什么我的删除功能有问题:

使用 int :

int delQ(Queue &Q) // => return int

如果我做:

struct delQ(Queue &Q) // => what do i return?

【问题讨论】:

  • 如何声明一个返回 int 的函数?现在你将如何声明相同的函数但返回另一种类型,例如结构?
  • 结构不是函数

标签: c++ function struct return-type


【解决方案1】:

您使用struct 定义的类型不是struct,而是dough

因此,如果您习惯使用int,但想使用新结构,只需将int 替换为dough

dough delQ(Queue &Q)  {
    dough retval;   // I don't know how you initialize your struct
    ....
    return retval;  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    相关资源
    最近更新 更多