c++ - 错误 : My every function & variable 'was not declared in this scope (code;;blocks, C++)

标签 c++ codeblocks

<分区>

我是 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;
    }

标题.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

函数.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;
   }

最佳答案

标题中函数声明的分号?哦,实际上缺少很多分号。请确保将它们插入到需要的位置,然后重试。

关于c++ - 错误 : My every function & variable 'was not declared in this scope (code;;blocks, C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15976608/

相关文章:

c++ - Unicode - 通常在 C++ 中使用它

c# - Visual Studio 2019 : Insert multiple C++ style single-line comments

c++ - 如何从 SuperBible 获取 GLTools 库以在 Ubuntu 中工作?还是另一种选择?

c++ - std::set 迭代顺序是否始终根据 C++ 规范升序?

c++ - 使用 std::atomic<bool>::atomic 时出现错误 C2248

c++ - 重载运算符+编译错误 '' 没有匹配的函数调用.. .''

c - 程序不返回 float 并根据使用的编译器给出不同的结果?

c++ - 在c++中将pcap文件解析为sip消息

c - 用文字打印数字的程序

c++ - 试图从 C++ 程序中的 openoffice 电子表格 ods 文件中提取 contents.xml