简单C++:如何在C++中全局化变量

Simple C++: How to globalise variables in C++

本文关键字:C++ 全局 变量 简单      更新时间:2023-10-16

当我试图构建程序时出错:"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 == falsegc == 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)
{
    // ...
}