错误:我的每个函数和变量都没有在这个范围内声明(代码;;块,C++)
Error: My every function & variable 'was not declared in this scope (code;;blocks, c++)
我是c++的新手,我写了我的第一个真正的程序(一个简单的四操作计算器)。当我编译它时,我得到了12个编译错误。我能够解决一些(缺少分号等),但其中7个拒绝消失。错误和我的代码在这里:
错误:
15 error: 'cout' was not declared in this scope
15 error: 'endl' was not declared in this scope
21 error: 'GetNumber' was not declared in this scope
24 error: 'GetOperator' was not declared in this scope
29 error: 'nAnswer' was not declared in this scope
33 error: 'AskContinue' was not declared in this scope
35 error: 'bContinue' was not declared in this scope
Main.cpp
//Four-operation calculator
//© Olli Utriainen 2013
#include <iostream>
//Header with functions GetNumber(), GetOperator() and AskContinue()
#include "calculator.h"
using namespace std;
int main()
{
//Main loop's condition
bool bContinue = false;
cout << "Welcome to Olli's calculator!" << endl << endl;
//Main loop
do
{
cout << "Give first number" << endl;
int nNumber1 = GetNumber();
cout << "Give operator (+,-,*,/)" << endl;
char cOperator = GetOperator();
cout << "Give second number" << endl;
int nNumber2 = GetNumber();
nAnswer = nNumber1 + nNumber2;
cout << nNumber1 << " " << cOperator << " " <<
nNumber2 << " = " << nAnswer << endl;
bContinue = AskContinue();
} while (bcontinue);
return 0;
}
Headers.h
//Headers for the calculator
//© Olli Utriainen 2013
#ifndef CALCULATOR_H_INCLUDED
#define CALCULATOR_H_INCLUDED
//Function for inputting numbers
int GetNumber()
//Function for inputting a mathematical operator (+,-,*,/)
char GetOperator()
//Function for asking Continue Yes/No
bool AskContinue()
#endif // CALCULATOR_H_INCLUDED
Functions.cpp
//Functions for the calculator
//© Olli Utriainen 2013
#include <iostream>
using namespace std;
//Function for inputting numbers
int GetNumber()
{
int nNumber
cin >> nNumber;
return nNumber
}
//Function for inputting a mathematical operator (+,-,*,/)
int GetOperator()
{
char cOperator
bool bValidOperator = false;
//Do-while loop for checking if given operator
//is valid (+,-,*,/). If invalid, bValidOperator
//is changed to false ->loop will break and the
//given operator is returned
do
{
cin >> cOperator;
if (cOperator == '+' || '-' || '*' || '/')
{
bValidOperator = true;
}
else
{
bValidOperator = false;
cout << "Invalid operator" << endl;
}
} while (!bValidOperator)
return cOperator;
}
//Function for asking Continue Yes/No
bool AskContinue()
{
cout << "New calculation? Y/N" << endl;
bool bContinue = false;
if (cin >> 'Y' || 'y')
{
bContinue = true;
}
if (cin >> 'N' || 'n')
{
bContinue = false;
}
return bcontinue;
}
标题中的函数声明分号?哦,实际上少了很多分号。请确保在需要的地方插入它们,然后再试一次。
大量错别字;大量分号丢失;
在calculator.cpp中,你应该#include "calculator.h"
in bool AskContinue()应该是:
{
cout << "New calculation? Y/N" << endl;
bool bContinue = false;
char choice;
cin >> choice;
if ((choice == 'Y')||(choice == 'y'))
{
bContinue = true;
}
if ((choice == 'N')||(choice == 'n'))
{
bContinue = false;
}
return bContinue;
}
这些是我现在能找到的
相关文章:
- 我的代码中有错误,未声明的标识符
- 在多个头文件中从接口声明被覆盖的函数时,如何避免重复代码?
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 代码作为 C 文件工作,但不作为C++文件,错误:'__builtin_types_compatible_p'未在此范围内声明
- 在C++代码中使用的 C 标头中向前声明枚举
- C++代码中奇怪的未使用的静态字符串声明
- 如何将char数组声明为函数参数?或告诉我此代码中还有其他问题?
- 使用模板化代码前向声明类
- 这是有效的C++代码吗?(在声明范围之外使用本地类型)
- 如何使用声明类型简化此代码
- 模板函数声明为 void - 代码不起作用?
- 代码块编译错误.Cin 未在此范围内声明
- 模板化友元声明在 G++ 5.4.0 下不起作用 - 编译器错误或错误代码
- 无法使用在模板化基类 [C++] 中声明的枚举编译代码
- 以下代码运行后,如何在程序再次运行之前清除用户声明的变量 empName
- 声明原型时如何在主代码块中调用函数
- 声明全局变量 <vector>,代码将无法成功运行
- 有没有修复我的代码没有将 int 声明为我在 if else 循环中选择的内容
- 将代码声明放入字中(引用操作符和解引用操作符混淆)
- 如何为Arduino代码声明C++函数