Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'was not declared in this scope' error

Tags:

c++

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.

like image 806
cortex Avatar asked Apr 07 '12 16:04

cortex


People also ask

How do I fix error not declared in this scope?

To resolve this error, a first method that is helpful would be declaring the function prototype before the main() method. So, we have used the function prototype before the main method in the updated code. When we have compiled the code, it throws no exceptions and runs properly.

How do I fix not declared in this scope Arduino?

In case of getting the Serial1 was not declared in this scope error, chances are your Arduino has no Serial1. Assuming that you use Arduino Uno, you need to comment the Serial1 and un-comment the SoftwareSerial part in order to solve the problem.

How do you declare a scope?

When you declare a program element such as a class, function, or variable, its name can only be "seen" and used in certain parts of your program. The context in which a name is visible is called its scope. For example, if you declare a variable x within a function, x is only visible within that function body.

What does was not declared in this scope in Arduino mean?

The error 'was not declared in this scope' generally occurs when a variable or function is not accessible to its call. A global variable in Arduino is the variable that is declared outside any function, including the setup() and loop() functions.


1 Answers

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

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:

//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; } 
like image 165
Haatschii Avatar answered Sep 22 '22 14:09

Haatschii