gpt4 book ai didi

c++ - 错误: ‘n’ was not declared in this scope

转载 作者:行者123 更新时间:2023-12-02 11:13:09 25 4
gpt4 key购买 nike

我编写了一个函数来查找数字n的除数之和。

int divisor_sum(long n) {
long sum = 0;
for (int a=1, a<=n, a++) {
if n % a == 0 {
sum = sum + a;
}
}
return sum;
}

不幸的是,该程序(包括一个主函数框架)无法编译,因为它说“在该范围内未声明”。我试过在函数定义语句之前和之后将n声明很长时间都无济于事。我该如何解决?
谢谢

最佳答案

就像StoryTeller和O'Neil在评论中告诉您的一样,您需要替换掉它

for (int a=1, a<=n, a++)


for (int a = 1; a <= n; a++)

和这个
if n % a == 0


if (n % a == 0)

关于c++ - 错误: ‘n’ was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48616959/

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