C++ 未在 Scope 中声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18347177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:54:51  来源:igfitidea点击:

Not declared in Scope

c++

提问by SkyVar

I have wrote a simple average calculation program trying to calculate a semester average. When I compile the code I get an error telling me my 'inputExam' function was not declared in this scope. I've researched the error message and I can't figure out what to do to fix it.

我写了一个简单的平均计算程序,试图计算一个学期的平均数。当我编译代码时,我收到一条错误消息,告诉我我的“inputExam”函数未在此范围内声明。我研究了错误消息,但不知道如何修复它。

I also get this error for the other functions, but once I understand my error I think I can fix the others.

我也收到其他函数的这个错误,但是一旦我理解了我的错误,我想我可以修复其他函数。

 #include <iostream>

using namespace std;

int main()
{
    double finalExam=0.0;
    double midterm = 0.0;
    double quizzes = 0.0;
    double labs = 0.0;
    double semGrade=0.0;

    midterm=inputExam("Midterm");
    finalExam=inputExam("Final");
    quizzes=inputAndAvgQuizzes();
    labs=inputAndAvgLabs();

    semGrade=(midterm*.2)+(finalExam*.2)+(labs*.5)+(quizzes*.1);

    cout<<"Your End of Semester Grade is: " semGrade;

    return 0;
}

double inputExam(string examType)
{
    double grade;
    cout<< "Enter the " examType " Score: ";
    cin>>grade;
    return (grade);
}

double inputAndAvgLabs()
{
    double num [4];
    double sum;
    double avg;

    if (int a=0, a<3,a++)
    {
        cout<<"What is the grade?"<<endl;
        cin>>num[a]>>endl;
    }
    if (int a=0, a<3, a++)
    {
        sum=sum+num[a];
    }
    avg=sum/4;

    return avg;
}

double inputAndAvgQuizzes()
{
    double num[3];
    double sum;
    double avg;
    double lowest = num[0];

    if (int a=0, a<2,a++)
    {
        cout<<"What is the grade?"<<endl;
        cin>>num[a]>>endl;
    }

    if (lowest>num[1])
    {
        lowest=num[1];
    }
    if (lowest>num[2])
    {
        lowest=num[2];
    }
    sum=num[1]+num[2]+num[3]-lowest;
    avg=sum/2;

    return avg;
}

回答by ChiefTwoPencils

You need to let it be known that those functions exist so add prototypes for your functions above mainor define your functions there. Like so:

您需要让人们知道这些函数存在,因此在上面为您的函数添加原型main或在那里定义您的函数。像这样:

...
double inputExam(string examType);
double inputAndAvgLabs();
double inputAndAvgQuizzes();

int main() { ... }

//definitions after main

..or copy paste all those definitions above the call to mainlike so:

..或复制粘贴调用上方的所有定义,main如下所示:

...
// Function defs here
// Prototypes no longer needed
...
int main() {...}
// Defs no longer needed here

Alternatively you can put all the definitions in an external file and compile it into the project via a makefile or better yet, and as you progress, create classes in header and implementation files and include them in your file the same way(sort of) that you do #include <iostream>.

或者,您可以将所有定义放在一个外部文件中,并通过make文件或更好的方式将其编译到项目中,并且随着您的进展,在头文件和实现文件中创建类并将它们以相同的方式(有点)包含在您的文件中你做#include <iostream>

Another small nugget of advice would be to avoid using namespace std;. If not only in theory it's bad practice and can lead to namespace clashing in larger projects. If you, like me, hate typing std::string ...then add using std::string;to your code for the same ease of use.

另一个小建议是避免using namespace std;. 如果不仅在理论上它是不好的做法,并且可能导致大型项目中的命名空间冲突。如果你像我一样讨厌打字,std::string ...那么添加using std::string;到你的代码中以获得同样的易用性。

回答by ilent2

In C/C++ you need to declare the function before you use it. In this case, it simply means declaring function prototypes before your main function and then implementing them after the main function.

在 C/C++ 中,您需要在使用之前声明该函数。在这种情况下,它只是意味着在 main 函数之前声明函数原型,然后在 main 函数之后实现它们。

Example:

例子:

// declare a prototype
double Function(int variable);

int main()
{
    Function(5);
    return 0;
}

// Implement the function
double Function(int variable)
{
    /* Do Something */
}

Alternatively, you could change your code to the form:

或者,您可以将代码更改为以下形式:

// Implement the function first
double Function(int variable)
{
    /* Do Something */
}

int main()
{
    Function("Testing");
    return 0;
}