所以我正在编写这个简单的程序,使用找到的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
它说我的变量未使用但后来说它没有声明?谁能告诉我什么是错的。
答案 0 :(得分:13)
变量的范围始终是它所在的块。例如,如果您执行类似
的操作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块
之外定义yint 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;
}
答案 1 :(得分:3)
以下是基于您的问题的简化示例:
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中。然后你尝试引用一个名为{1}的变量。 y
结束后的if
,而不是这样的变量y
,因为该范围内不存在此类变量。
您可以通过将y
放在包含对它的所有引用的最外层作用域来解决问题:
int y;
if (test)
{
y = 1;
}
else
{
y = 2;
}
int x = y;
我用简化的代码编写了示例,以便您更清楚地了解问题。您现在应该可以将该原则应用于您的代码。
答案 2 :(得分: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;
}
答案 3 :(得分:1)
下面
{int y=((year-1)%100);int c=(year-1)/100;}
你声明并初始化变量y, c
,但是在它们用完范围之前你根本不使用它们。这就是你收到unused
消息的原因。
稍后在函数中,y, c
是未声明的,因为您所做的声明只保留在它们所在的块内(大括号{...}
之间的块)。
答案 4 :(得分:-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;
}