C++ “未在此范围内声明”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10056093/
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 13:33:31  来源:igfitidea点击:

'was not declared in this scope' error

c++

提问by cortex

So I was writing this simple program to calculate the day of any date using the Gaussian algorithm found here.

所以我正在编写这个简单的程序来使用此处找到的高斯算法计算任何日期的日期。

#include <iostream>
using namespace std;

//Using the Gaussian algorithm
int dayofweek(int date, int month, int year ){
    int d=date;
    if (month==1||month==2)
        {int y=((year-1)%100);int c=(year-1)/100;}
    else
        {int y=year%100;int c=year/100;}
    int m=(month+9)%12+1;
    int product=(d+(2.6*m-0.2)+y+y/4+c/4-2*c);
    return product%7;
}

int main(){
    cout<<dayofweek(19,1,2054);
    return 0;
}

It's a very simple program and what's even more puzzling is the output.

这是一个非常简单的程序,更令人费解的是输出。

:In function  dayofweek(int, int, int)':
:19: warning:  unused variable ‘y'
:19: warning: unused variable ‘c'
:21: warning: unused variable ‘y'
:21: warning: unused variable ‘c'
:23: error: ‘y' was not declared in this scope
:25: error: ‘c' was not declared in this scope

It says that my variable is unused but then says that it isn't declared? Could anyone please tell me whats wrong.

它说我的变量未使用但又说它没有声明?任何人都可以请告诉我出了什么问题。

采纳答案by Haatschii

The scope of a variable is always the block it is inside. For example if you do something like

变量的作用域始终是它所在的块。例如,如果你做类似的事情

if(...)
{
     int y = 5; //y is created
} //y leaves scope, since the block ends.
else
{
     int y = 8; //y is created
} //y leaves scope, since the block ends.

cout << y << endl; //Gives error since y is not defined.

The solution is to define y outside of the if blocks

解决方案是在 if 块之外定义 y

int y; //y is created

if(...)
{
     y = 5;
} 
else
{
     y = 8;
} 

cout << y << endl; //Ok

In your program you have to move the definition of y and c out of the if blocks into the higher scope. Your Function then would look like this:

在您的程序中,您必须将 y 和 c 的定义从 if 块中移到更高的范围内。您的函数将如下所示:

//Using the Gaussian algorithm
int dayofweek(int date, int month, int year )
{
    int y, c;
    int d=date;

    if (month==1||month==2)
    {
         y=((year-1)%100);
         c=(year-1)/100;
    }
    else
    {
         y=year%100;
         c=year/100;
    }
int m=(month+9)%12+1;
int product=(d+(2.6*m-0.2)+y+y/4+c/4-2*c);
return product%7;
}

回答by David Heffernan

Here's a simplified example based on of your problem:

这是一个基于您的问题的简化示例:

if (test) 
{//begin scope 1
    int y = 1; 
}//end scope 1
else 
{//begin scope 2
    int y = 2;//error, y is not in scope
}//end scope 2
int x = y;//error, y is not in scope

In the above version you have a variable called ythat is confined to scope 1, and another different variable called ythat is confined to scope 2. You then try to refer to a variable named yafter the end of the if, and not such variable ycan be seen because no such variable exists in that scope.

在上面的版本,你有一个名为变量y被限制在范围1,和另一个叫不同的变量y被限制在范围2.然后尝试引用一个命名变量y的结束后if,并没有这样的变化y可以看出因为在那个范围内不存在这样的变量。

You solve the problem by placing yin the outermost scope which contains all references to it:

您可以通过放置y包含所有引用的最外层作用域来解决问题:

int y;
if (test) 
{
    y = 1; 
}
else 
{
    y = 2;
}
int x = y;

I've written the example with simplified made up code to make it clearer for you to understand the issue. You should now be able to apply the principle to your code.

我已经用简化的代码编写了示例,以使您更清楚地了解问题。您现在应该能够将原则应用于您的代码。

回答by huysentruitw

You need to declare y and c outside the scope of the if/else statement. A variable is only valid inside the scope it is declared (and a scope is marked with { })

您需要在 if/else 语句的范围之外声明 y 和 c。变量只在它声明的范围内有效(范围用 { } 标记)

#include <iostream> 
using namespace std; 
//Using the Gaussian algorithm 
int dayofweek(int date, int month, int year ){ 
int d=date; 
int y, c;
if (month==1||month==2) 
        {y=((year-1)%100);c=(year-1)/100;} 
else 
        {y=year%100;c=year/100;} 
int m=(month+9)%12+1; 
int product=(d+(2.6*m-0.2)+y+y/4+c/4-2*c); 
return product%7; 
} 

int main(){ 
cout<<dayofweek(19,1,2054); 
return 0; 
} 

回答by Imp

Here

这里

{int y=((year-1)%100);int c=(year-1)/100;}

you declare and initialize the variables y, c, but you don't used them at all before they run out of scope. That's why you get the unusedmessage.

您声明并初始化变量y, c,但在它们用完范围之前根本不使用它们。这就是您收到unused消息的原因。

Later in the function, y, care undeclared, because the declarations you made only hold inside the block they were made in (the block between the braces {...}).

在函数的后面,y, c是未声明的,因为您所做的声明仅保存在它们所在的块内(大括号之间的块{...})。

回答by Hafize

#include <iostream>
using namespace std;
class matrix
{
    int a[10][10],b[10][10],c[10][10],x,y,i,j;
    public :
        void degerler();
        void ters();
};
void matrix::degerler()
{
    cout << "Sat?rlar? giriniz: "; cin >> x;
    cout << "Sütunlar? giriniz: "; cin >> y;
    cout << "?lk matris elamanlar?n? giriniz:\n\n";
    for (i=1; i<=x; i++)
    {
        for (j=1; j<=y; j++)
        {
            cin >> a[i][j];
        }
    }
    cout << "?kinci matris elamanlar?n? giriniz:\n\n";
    for (i=1; i<=x; i++)
    {
        for (j=1; j<=y; j++)
        {
            cin >> b[i][j];
        }
    }
}

void matrix::ters()
{
    cout << "matrisin tersi\n";
    for (i=1; i<=x; i++)
    {
        for (j=1; j<=y; j++)
        {
    if(i==j)
    {
    b[i][j]=1;
    }
    else
    b[i][j]=0;
    }
}
float d,k;
    for (i=1; i<=x; i++)
    {
    d=a[i][j];
        for (j=1; j<=y; j++)
        {
    a[i][j]=a[i][j]/d;
            b[i][j]=b[i][j]/d;
    }
        for (int h=0; h<x; h++)
        {
            if(h!=i)
    {
       k=a[h][j];
               for (j=1; j<=y; j++)
               {
                    a[h][j]=a[h][j]-(a[i][j]*k);
                    b[h][j]=b[h][j]-(b[i][j]*k);
               }
    }
    count << a[i][j] << "";
    }
    count << endl;
}  
}
int main()
{
    int secim;
    char ch;    
    matrix m;
    m.degerler();
    do
     {
    cout << "se?iminizi giriniz\n";
    cout << " 1. matrisin tersi\n";
    cin >> secim;
    switch (secim)
    {
        case 1:
            m.ters();
            break;
    }
    cout << "\nBa?ka bir ?ey yap/n?";
    cin >> ch;
    }
    while (ch!= 'n');
    cout << "\n";
    return 0;
}