C++ c++‘;总计’;未在此作用域中声明。主要 #包括 使用名称空间std; 整数人口(整数当前流行) { 整数时间; 婴儿出生; 国际移民; 死亡; 整数合计; 时间=24*60*60*365;//从一天到一秒的转换 出生=时间/8; 死亡=时间/12; 移民=时间/33;//计算死亡、出生、移民的比率 总计=出生+移民死亡+当前_pop;//求和 返回总数; } int main(){ int current_pop=1000000; 总体(当前流行);//测试 cout

C++ c++‘;总计’;未在此作用域中声明。主要 #包括 使用名称空间std; 整数人口(整数当前流行) { 整数时间; 婴儿出生; 国际移民; 死亡; 整数合计; 时间=24*60*60*365;//从一天到一秒的转换 出生=时间/8; 死亡=时间/12; 移民=时间/33;//计算死亡、出生、移民的比率 总计=出生+移民死亡+当前_pop;//求和 返回总数; } int main(){ int current_pop=1000000; 总体(当前流行);//测试 cout,c++,function,main,declare,C++,Function,Main,Declare,不同函数中具有相同名称的变量存储不同的值。它们完全不相关。开始编程时需要一些习惯。尝试以下方法: #include <iostream> using namespace std; int population(int current_pop) { int Time; int birth; int immigrant; int death; int total; Time = 24*60*60*365; // convet day to secon

不同函数中具有相同名称的变量存储不同的值。它们完全不相关。开始编程时需要一些习惯。尝试以下方法:

#include <iostream>
using namespace std;

int population(int current_pop)
{
   int Time;
   int birth;
   int immigrant;
   int death;
   int total;
   Time = 24*60*60*365; // convet day to second
   birth = Time/8;
   death = Time/12;
   immigrant = Time/33; // calculate the rate of the death, birth,immigrant
   total = birth+immigrant-death+current_pop; // make sum
   return total;
}

int main() {
   int current_pop = 1000000;
   population(current_pop); //test
   cout << total << endl;
}

不同函数中具有相同名称的变量存储不同的值。它们完全不相关。开始编程时需要一些习惯。请尝试以下方法:

#include <iostream>
using namespace std;

int population(int current_pop)
{
   int Time;
   int birth;
   int immigrant;
   int death;
   int total;
   Time = 24*60*60*365; // convet day to second
   birth = Time/8;
   death = Time/12;
   immigrant = Time/33; // calculate the rate of the death, birth,immigrant
   total = birth+immigrant-death+current_pop; // make sum
   return total;
}

int main() {
   int current_pop = 1000000;
   population(current_pop); //test
   cout << total << endl;
}

正如错误所说,您在
population()
函数中声明了
total
变量,该变量在
main()
函数中不可用。
population()
函数将有其自己的作用域,因此在该函数中声明的任何变量都只能在那里访问。
要使其可用,则需要在
main()
中声明它


正如错误所说,您在
population()
函数中声明了
total
变量,该变量在
main()
函数中不可用。
population()
函数将有其自己的作用域,因此在该函数中声明的任何变量都只能在那里访问。
要使其可用,则需要在
main()
中声明它


从中返回后,填充函数甚至不存在。剩下的就是返回值,在本例中,返回值包含您要查找的信息。您应该阅读一些关于c++中作用域的文章。从中返回后,填充函数甚至不存在。剩下的就是返回值,在本例中,返回值具有您正在寻找的信息。您应该阅读一些关于c++
cout
cout中范围的文章
int main() {
    ...
    int total = population(current_pop);
    ...
}