c++ - 言語 - 制御の転送は次の初期化をバイパスします goto
'このスコープで宣言されていませんでした'エラー (4)
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;
}
だから、 here見つかったガウスアルゴリズムを使って、日付を計算する簡単なプログラムを書いていまし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
私の変数は使用されていませんが、宣言されていないと言いますか? 誰でも何が間違っているか教えてもらえますか?
あなたの問題に基づいた簡単な例を次に示します。
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
上記のバージョンでは、スコープ1に限定されたy
という変数と、スコープ2に限定されたy
という別の変数があります。次に、 if
という名前の変数を参照しようとします。その変数にその変数が存在しないため、変数y
が見えます。
あなたは、すべての参照を含む最も外側のスコープにy
を置くことによって問題を解決します:
int y;
if (test)
{
y = 1;
}
else
{
y = 2;
}
int x = y;
私は、あなたが問題を理解することをより明確にするために、単純化されたコードで例を書いた。 これで、コードに原則を適用できるようになりました。
変数のスコープは、常にそれが内部にあるブロックです。 たとえば、次のようなことをすれば
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
あなたのプログラムでは、ifブロックのyとcの定義をより高いスコープに移動する必要があります。 あなたの関数は次のようになります:
//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;
}
#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;
}