Was Not Declared in This Scope' Error

was not declared in this scope' error

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

C++ error: 'variable' was not declared in this scope

The reason for the error is that you're declaring each of klasa and oznaka inside the "if statement", as such you can only reach these variables within their scope (i.e inside each of their respective "if statements")

#include <iostream>
#include <string>
using namespace std;

int main(){
int T;
cin >> T;
string klasa = "";
string oznaka = "";

if(T>=73){
klasa = "A";
oznaka = "XL";
}
else if(63<=T && T<73){
klasa = "A";
oznaka = "L";
}
else if(53<=T && T<63){
klasa = "A";
oznaka = "M";
}
else if(43<=T && T<53){
klasa = "A";
oznaka = "S";
}
else if(T<43){
klasa = "B";
oznaka = "XS";
}

cout << klasa << " " << oznaka;
}

The code above places the declaration of the variables in a more global scope, so you can access them inside the "if statements" and outside of them too.

error: ‘leftHeight’ was not declared in this scope

The problem is that you have defined leftHeight inside if else block and therefore they are not visible outside of those blocks. Similarly the variable rightHeight is also visible only inside the blocks in which it was defined.

To solve this just define leftHeight outside(of those blocks) once and then just assign value to them inside if and else blocks which would look like:

void computeHeight(Node *n) {
int leftHeight = -1;//define and initialize leftHeight
int rightHeight = -1;//define and initialize rightHeight
// Implement computeHeight() here.
if (n->left) {
computeHeight(n->left);
leftHeight = n->left->height;//note this is assignment
}
if (n->right) {
computeHeight(n->right);
rightHeight = n->right->height;//note this is assignment
}
n->height = std::max(leftHeight, rightHeight) + 1;//this works now
}

C++ Error: class/object was not declared in this scope

You don't have to create a variable inside a class pointing to the object of that class you are using. It is already declared and is called this.

With operator -> you can then access member variables from this. Like so:

#include <iostream>
#include <string>

using namespace std;

class Player
{
public:
int health;

// this is constructor
Player(int health_at_start)
{
this->health = health_at_start;
}

void comCheckStats()
{
cout << this->health << '\n';
}
};

int main()
{
// create player with 100 health
Player p1(100);
p1.comCheckStats();

// create player with 200 health
Player p2(200);
p2.comCheckStats();
}

As you can see, I am using something called constructor to create new instance of Player. It is just function without return type, declared with the same name as the class. It initializes member variable starting data and you can pass some values to it too.

system' was not declared in this scope error

Just include stdlib.h, but don't use system("pause") as it's not standard and will not work on every system, just a simple getchar() (or a loop involving getchar() since you've used scanf()) should do the trick.

And normally system("pause") is found in windows command line programs because windows command prompt closes when the program exits, so maybe running the program from the command prompt directly would help, or using an IDE that fixes this like geany.

Finally always check the return value if scanf() instead of assuming that it worked.

Note: This code

return topla(n - 1) + topla(n - 1) + topla(n - 1)

you can write as

return 3 * topla(n - 1);

instead of calling topla() recursively 3 times.

And you don't really need the else because the function returns unless the n != 1 so even without the else the recursion will stop when n == 1.

Not declared in the scope error, C++ sfml

Just a quick introduction to scopes in c++. There are a few different ways scoping can work, depending on the type of variable you're using but in general most variables have an automatic scope which means they are created and destroyed automatically. The rules of thumb are:

  1. Any two functions are not correlated with each other in any way. Neither can see what's inside the other.
  2. Any variable with an automatic scope is created where you created, and is deleted at the end of the body you created it in.

examples:

void someFunc()
{//body start of someFunc

if ()
{ //body start of if

while()
{
//body of while
}

} //body end of if

}// body end of someFunc

int main() //head of main. main cant see inside body of someFunc. vice versa
{ //body start of function main

} // body end of function main
int main()
{
int abc = 4; //abc was created in main body thus exists until main body's end
if ()
{
float bcd; //bcd was created in if body thus...
}//float exists until this point etc.

}//abc exists until this point
//Pretty much the only exception are dynamic variables in basic C++:
int main()
{
int *dyn = new int;
//important!! the dynamic part here is the 'new int' thats the non-automatic scoped variable.
//The int *dyn is an automatic pointer to that scope. after main function ends the pointer is
//automatically deleted yet the value of the variable still exists in that place in
//memory until you delete it manually or the program ends completely. not deleting a non-automatic
//scoped variable ends in memory leaks which lead to unnecessary higher resource usage and
//slowness of the systme running the program.
}
//So finally lets say you create a class with some values or something. Lets say they're all automatically scoped.
1. you can access variables in the class itself.
2. you can't access variables inside functions.

//Lets use your example:
//to make it easier to understand lets just make it a simple function instead of a class method(same thing but still)
void test()
{
sf::Texture mangoTexture;
if (!mangoTexture.loadFromFile("Data/mango.png"))
{
std::cout << "Load Error";
}

sf::Sprite mango;
mango.setTexture(mangoTexture);

}

int main()
{
//lets say you want to use mango in main. This breaks rule 1 of the aformenthioned 2 rules i wrote at the start:
main function cannot see whats inside test function. And even if it somehow could, it couldnt use that variable mango because it was destroyed at the end of the function:

//mango doesnt exist hasnt been made yet
test();
//mango doesnt exist. has been made and deleted in prev line.

//In the end functions are just packed up code in neat blocks you can recycle and use many times
return 0;
}
//If you want to actually use mango in eg main, youd have to either have it as a class variable or have the function return mango

//OPTION 1
class Game
{
public:
sf::Sprite mango;
void test()
{
sf::Texture mangoTexture;
if (!mangoTexture.loadFromFile("Data/mango.png"))
{
std::cout << "Load Error";
}


mango.setTexture(mangoTexture);
}
};

//OPTION 2
class Game
{
public:
sf::Sprite test()
{
sf::Texture mangoTexture;
if (!mangoTexture.loadFromFile("Data/mango.png"))
{
std::cout << "Load Error";
}

sf::Sprite mango;
mango.setTexture(mangoTexture);
return mango;
}
};

//For your case i highly recommend option 1. I wrote both options just as an example as there can be many difficult situations in which you might be forced to only use one or the other.



Related Topics



Leave a reply



Submit