简单C++:如何在C++中全局化变量
Simple C++: How to globalise variables in C++
当我试图构建程序时出错:"error:"celsius(("未在此作用域"中声明
现在,如果我错了,请纠正我,但我认为问题是,因为当我在fahrenheit函数中调用函数"fahrenhait"时,它在我的另一个函数"celsius"之前,所以它不起作用。现在,切换它们很简单,但在celsius函数中也调用了fahrenheit。
在python中,你所需要做的就是用"global"语法将其全局化,那么C++的等价物是什么?
感谢
PS。如果你想要的话,这是我的代码。
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int fahrenheit(){
system("CLS");
cout << "-----------------------------------------------";
cout << "nYOU HAVE CHOSEN FAHRENHEIT TO CELSIUS MODE";
cout << "n----------------------------------------------";
bool again;
again = true;
while (again == true){
int tempurf;
cout << "nFahrenheit Temperature to be Converted: ";
cin >> tempurf;
int tempurc;
tempurc = tempurf - 32;
tempurc = tempurc * 5;
tempurc = tempurc / 9;
cout << "nn" << tempurf << " F is " << tempurc << " C";
cout << "nnnnWHAT WOULD YOU LIKE TO DO: ";
cout << "n - ANOTHER CONVERSION TYPE A";
cout << "n - FOR CELSIUS MODE TYPE C";
cout << "n - TO EXIT TYPE E";
bool goodc;
goodc = false;
while (goodc == false){
string choosing;
cout << "n ";
cin >> choosing;
if (choosing == "A" or choosing == "a"){
system("CLS");
goodc = true;
}
else if (choosing == "C" or choosing == "c"){
goodc = true;
again = false;
celsius();
}
else if (choosing == "E" or choosing == "e"){
goodc = true;
again = false;
return 0;
}
else{
cout << "n Invalid Choice";
}
}
}
}
int celsius(){
system("CLS");
cout << "---------------------------------------------";
cout << "nYOU HAVE CHOSEN CELSIUS TO FAHRENHEIT MODE";
cout << "n---------------------------------------------";
bool again;
again = true;
while (again == true){
int tempuc;
cout << "nCelsius Temperature to be Converted: ";
cin >> tempuc;
int tempuf;
tempuf = tempuc * 9;
tempuf = tempuf / 5;
tempuf = tempuf + 32;
cout << "nn" << tempuc << " C is " << tempuf << " F";
cout << "nnnnWHAT WOULD YOU LIKE TO DO: ";
cout << "n - ANOTHER CONVERSION TYPE A";
cout << "n - FOR FAHRENHEIT MODE TYPE F";
cout << "n - TO EXIT TYPE E";
bool goodc;
goodc = false;
while (goodc == false){
string choosing;
cout << "n ";
cin >> choosing;
if (choosing == "A" or choosing == "a"){
system("CLS");
goodc = true;
}
else if (choosing == "F" or choosing == "f"){
goodc = true;
again = false;
fahrenheit();
}
else if (choosing == "E" or choosing == "e"){
goodc = true;
again = false;
return 0;
}
else{
cout << "n Invalid Choice";
}
}
}
}
int main(){
cout << "Welcome to the Fahrenheit/Celsius Converter!";
cout << "n By Ben Sarachi";
cout << "nnWhich way would you like to convert to:";
cout << "n - If you would like Fahrenheit to Celsius please type F";
cout << "n - If you would like Celsius to Fahrenheit please type C";
// GC stands for good choice
bool gc;
gc = false;
while (gc == false){
string choice;
cout << "n ";
cin >> choice;
//Call Functions
if (choice == "F" or choice == "f"){
gc = true;
fahrenheit();
}
else if (choice == "C" or choice == "c"){
gc = true;
celsius();
}
else{
cout << "Invalid Choice";
}
}
}
您希望为函数添加一个正向声明,以便编译器知道函数的存在。现在的情况是,华氏温度正在调用摄氏度,但编译器不知道摄氏度是多少。
在代码的顶部,在包含的内容下方添加以下内容:
int fahrenheit();
int celsius();
这告诉编译器,您将在某个时刻定义这些函数。
然后,您可以在您喜欢的文件中以任何顺序声明您的函数。
此外,为了将来参考,该正向声明应该具有与您的函数相同的签名。所以,如果你有一个功能,比如:
void foo(int bar) { ... }
那么你的远期申报是:
void foo(int);
您需要的是函数前向声明。在定义fahrenheit
函数之前放入以下字符串:
int celsius();
这将告诉编译器,这样的函数存在并且具有以下原型。但该机构将在稍后推出。
您会得到错误,因为在编译fahrenheit()
函数时,celsius()
是未知的。你必须转发申报。
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int celsius(); // this is the forward declaration
int fahrenheit(){
// do something
celsius();
}
int celsius(){
// implement the function
}
另一种选择是创建一个类,并将这两个函数作为该类的成员。那么就不需要正向声明了(尽管声明类可以说是另一种形式(。
您的代码中还有其他几个问题:
- 函数设置为返回int,但并非所有路径都返回值
or
在这里是什么choice == "F" or choice == "f"
?它在某个地方被定义为||
吗- 不使用诸如
gc == false
或gc == true
之类的条件。优选使用gc
和!gc
,如同在if(gc)
或while(!gc)
中一样
将函数声明与定义分离。将您的代码更改为这样的代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
int celsius();
int fahrenheit();
int fahrenheit()
{
// ...
}
int celsius()
{
// ...
}
int main(int argc, char** argv)
{
// ...
}
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 当vector是tje全局变量时,c++中vector的内存管理
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何创建一个空的全局类并在启动时实例化它
- 内联函数中具有内部链接的全局变量
- 为什么虚函数不能是静态的和全局的?
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 全局作用域中函数指针的赋值
- g++用户定义的动态链接库上的全局new和delete运算符
- 在命名空间中使用全局命名空间中的函数
- 全局变量 多读取器 一个写入器多线程安全?
- 类的全局对象和静态成员
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- 如果全局变量默认是外部变量,为什么要添加"extern"关键字?
- 不同作用域中的静态变量和全局变量
- 如何使用 llvm-10 库在C++定义 LLVM 全局值变量?
- 使用全局声明的向量时,C++双重释放错误/损坏
- 将线程中的数据存储到全局容器的最佳方法?
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面