【问题标题】:'was not declared in this scope' error“未在此范围内声明”错误
【发布时间】:2012-04-20 19:34:42
【问题描述】:

所以我正在编写这个简单的程序来使用 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;
}

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

: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

它说我的变量未使用但又说它没有声明?谁能告诉我怎么了。

【问题讨论】:

  • 局部变量在声明它们的 {} 块之外不可见。

标签: c++


【解决方案1】:

您需要在 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; 
} 

【讨论】:

  • 您可能需要输入说明为什么这样可以解决问题。
【解决方案2】:

这里

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

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

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

【讨论】:

    【解决方案3】:

    变量的范围总是它所在的块。例如,如果您执行类似的操作

    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.
    

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

    int y; //y is created
    
    if(...)
    {
         y = 5;
    } 
    else
    {
         y = 8;
    } 
    
    cout << y << endl; //Ok
    

    在您的程序中,您必须将 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;
    }
    

    【讨论】:

      【解决方案4】:

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

      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
      

      在上面的版本中,您有一个名为 y 的变量被限制在范围 1 中,以及另一个名为 y 的变量被限制在范围 2 中。然后您尝试在之后引用一个名为 y 的变量if 的结尾,并且看不到 y 这样的变量,因为该范围内不存在这样的变量。

      您可以通过将y 放在包含所有对它的引用的最外层范围内来解决问题:

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

      我使用简化的代码编写了示例,以便您更清楚地理解问题。您现在应该可以将这一原则应用到您的代码中了。

      【讨论】:

        【解决方案5】:
        #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;
        }
        

        【讨论】:

          【解决方案6】:

          我只是想补充一点,对我来说,这个错误是由于文件头保护中的一个粗心错误造成的。而不是:

          #ifndef MY_AWESOME_HEADER_H
          #define MY_AWESOME_HEADER_H
          ...
          #endif // MY_AWESOME_HEADER_H
          

          我有

          #ifdef MY_AWESOME_HEADER_H
          #define MY_AWESOME_HEADER_H
          ...
          #endif // MY_AWESOME_HEADER_H
          

          【讨论】: