gpt4 book ai didi

c++ - 简单 C++ : How to globalise variables in C++

转载 作者:行者123 更新时间:2023-11-27 23:03:56 27 4
gpt4 key购买 nike

当我尝试构建我的程序阅读时出现错误:'错误:'celsius()' 未在此范围内声明'

现在,如果我错了,请纠正我,但我认为问题在于,当我在华氏度函数中调用它时,函数“华氏度”出现在我的另一个函数“摄氏度”之前,它不会起作用。现在,切换它们就足够简单了,但是在摄氏度函数中也调用了华氏度。

在 python 中,您需要做的就是使用“全局”语法将其全局化,那么 C++ 的等价物是什么?

谢谢

附言。如果需要,这是我的代码。

#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 << "\n\n" << tempurf << " F is " << tempurc << " C";
cout << "\n\n\n\nWHAT 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 << "\n\n" << tempuc << " C is " << tempuf << " F";
cout << "\n\n\n\nWHAT 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 << "\n\nWhich 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);

关于c++ - 简单 C++ : How to globalise variables in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25040432/

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