我尝试构建程序时遇到错误: '错误:'celsius()'未在此范围内声明'
现在,纠正我,如果我错了,但我认为问题是,因为当我在华氏温度功能中调用它时,函数'fahrenheit'出现在我的其他函数'celsius'之前,它将无效。现在,切换它们很简单,但是在摄氏功能中也会调用华氏度。
在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 << "\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";
}
}
}
答案 0 :(得分:4)
您希望为函数添加前向声明,以便编译器知道该函数存在。发生了什么事情,华氏称呼Celsius,但编译器不知道Celsius在那时是什么。
在代码的顶部,添加以下内容:
int fahrenheit();
int celsius();
这告诉编译器你将在某个时候定义这些函数。
然后,您可以在您喜欢的文件中以任何顺序声明您的功能。
此外,为了将来参考,该前向声明应与您的函数具有相同的签名。所以如果你有这样的功能:
void foo(int bar) { ... }
然后你的前瞻声明将是:
void foo(int);
答案 1 :(得分:0)
您需要的是功能转发声明。在定义fahrenheit
函数之前放置以下字符串:
int celsius();
这将告诉编译器,这样的函数存在并具有以下原型。但是身体将在稍后介绍。
答案 2 :(得分:0)
您收到错误,因为在编译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
}
另一种方法是创建一个类并将这两个函数作为该类的成员。那么你不需要前向声明(尽管声明一个类可以说是另一种形式)。
您的代码中还有其他几个问题:
or
choice == "F" or choice == "f"
是什么?是#defined在||
?gc == false
或gc == true
等条件。更喜欢在gc
或!gc
if(gc)
和while(!gc)
答案 3 :(得分:0)
从定义中分离功能声明。 将您的代码更改为以下内容:
#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)
{
// ...
}