C 链接列表和总和问题
C++ Linked list and sum issue
我在下面的代码上不断遇到错误,我不知道为什么。
int sum(struct node *head)
{
int total = 0;
struct node *temp = head;
while (temp != NULL)
{
total += temp->data;
temp = temp->next;
}
}
错误c4716'sum':必须返回值
就像错误消息所说的那样,您需要return
statment:
int sum(struct node *head)
{
int total = 0;
struct node *temp = head;
while (temp != NULL)
{
//cout << temp->data << 'n'; //debug
total += temp->data;
temp = temp->next;
}
return total; // <-- add this!
}
在编写int sum(struct node *head)
时,这意味着您的功能应返回整数值。因此,您可以做的是您可以在功能结束时添加返回语句。
类似的东西
int sum(struct node *head)
{
int total = 0;
struct node *temp = head;
while (temp != NULL)
{
total += temp->data;
temp = temp->next;
}
return total;
}
和您称之为此函数的语句只需将该函数分配给任何整数变量。
int t = sum(head);
希望对
相关文章:
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- C++关于ENUM的问题。我得到的响应比枚举列表大
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- 在C++中循环访问自定义结构列表的小问题
- 关于Stroustrup C++书的自动和{}列表问题
- 将值推送到 getter 函数调用的列表中时出现问题
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 在 c++ 中访问列表向量时出现问题
- 有关具有编译错误的操纵列表的问题
- C++:继承和列表函数作为类成员的问题
- C++ 入门加列表:10.9 "顶部"指针问题
- 尝试使用初始值设定项列表构造"std::vector"的问题
- 自己的C++列表类实现(插入函数)出现问题
- 链接的列表字符串分类问题
- C 链接列表和总和问题
- C++有问题的函数,数组到列表
- 从一系列问题列表中生成输入(x)问题
- 析构函数问题 + 列表显示
- 显示条件有问题?(列表中的对象)
- 信号发出问题-列表视图没有显示完整的列表