2012-04-07 230 views
15

因此,我正在編寫這個簡單的程序,使用發現的高斯算法計算任何日期的日期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 

它說,我的變量是未使用,但然後說它沒有聲明?任何人都可以告訴我什麼是錯的。

+1

局部變量在聲明它們的'{}'塊之外是不可見的。 – DCoder 2012-04-07 16:18:04

回答

7

變量的範圍始終是它所在的塊。例如,如果你這樣做

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塊

int y; //y is created 

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

cout << y << endl; //Ok 

在你的程序,你必須移動y的定義和c出來的,如果的外側y塊進入更高的範圍。那麼你的函數應該是這樣的:

//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; 
} 
+0

謝謝:D我現在明白了。 – cortex 2012-04-07 16:27:29

2

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

您可能想要輸入描述爲什麼這可以解決問題。 – 2012-04-07 16:18:47

+0

謝謝反正:D – cortex 2012-04-07 16:27:37

1

這裏

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

您聲明和初始化變量y, c,但你不使用他們完全沒有超出範圍。這就是爲什麼你得到unused消息。

在函數的後面,y, c是未聲明的,因爲你所做的聲明只保存在它們所在的塊內(大括號{...}之間的塊)。

2

下面是根據你的問題的一個簡單的例子:

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中。然後在if結束後嘗試引用名爲y的變量,並且不能看到此變量y,因爲該範圍中不存在此類變量。

您放置在y包含所有引用的最外層範圍內解決問題:

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

我已經寫了使用簡體由代碼的例子,使其更清晰,爲您瞭解問題。您現在應該能夠將該原則應用於您的代碼。

+0

感謝:D它現在有效。 – cortex 2012-04-07 16:28:15

-2
#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; 
}