• Register
1 vote


I simply want to know what is the Scope of variables in c++, and why the program throws back errors like, “c++ was not declared in this scope”?

Please keep your words as simple as possible.


12 7 7
15,250 points

1 Answer

0 votes


In programming, the scope of a variable is defined as the extent of the program code within which the variable can we accessed or declared or worked with.

Global and Local are the two types of variable scope. So what are these variables?

Global Variables: Global variables are usually declared outside of all of the functions and blocks, at the top of the program. They can be accessed from any portion of the program.

Local Variables: Local variables are declared inside a block. Local variables can only be declared on in-between ‘{‘ and ‘}’ or inside a block. Moreover, this variable only accessible inside the block.

Coming to the second part of your question, why does this happen? Before that,  have a look into the program below:

using namespace std; 
void func() 
    // this variable is local to the 
    int weight=20;     
int main() 
    cout<<"Weight is: "<<weight; 
    return 0; 

I’ve declared a local variable weight, and trying to call it outside from the particular function. As we know the local variables are only accessible inside his function.

In this case, if we try to compile and run this program it will throw back the error:

Error: weight was not declared in this scope

I hope this helps.

13 9 6
94,240 points

Related questions

1 vote
1 answer 17 views
Problem: I am a little curious about java variables. I was trying to figure out how does it work if I don&rsquo;t initialize a variable after defined. I assumed if the variable is an integer it could be equivalent to 0 and for String, it could be a null ... use a local variable before it has been given a value: Can anybody explain the thing clearly to me? Your answers are appreciated. Thanks.
asked Mar 19 Gavin 15.3k points
0 votes
1 answer 15 views
Code: #include <iostream>     #include <cstdlib>     using namespace std;     int ** mbd (int f, int c) {         int ** m = new int * [f];         for (int i = 0; i <f; i ++) {* (m + i) = new int [c]; }         return m;   ... using functions, and declare the array like this: int m [3] [5] (instead of as done in function mbd ()) . Is there any other solution where I can keep using the functions?
asked Aug 15 game 4.6k points
0 votes
1 answer 8 views
I am making a simple program that treats general trees as binary trees in C ++ on console; the user can insert nodes by typing the 'address' of the parent node using Dewey decimal notation (1.2, 1.1.1 and so on). So to find the address of that node I am capturing ... and not a copy, and therefore the values should change when I show the vector in main, but I don't know what I'm doing wrong. .
asked Aug 14 game 4.6k points
1 vote
1 answer 78 views
Problem: I am trying to create functions and use them in my c++ codes. I have created a function to add to integer numbers in a function. But the function is not working as it is supposed to do. I am attaching the code snippet below. Below have a look at my code and fix the ... int sum(int a, int b) { int result; result = a + b; return (result); } Please note that I am a newbie in programming.
asked May 29 adamSw 11.3k points
0 votes
1 answer 5 views
Problem: Hi there, I am a noob and recently while studying functions I came across the following program that created confusion in my mind: #include<iostream> using namespace std; void add(int a, int b) { cout<<"The sum of "<<a<<" and "<<b<<" is "<<a+b; } ... ,5,6); return 0; } First of all, why are all the functions named as add? Secondly, how does the compiler identifies which function to call?
asked Oct 14 Sheeza 2.9k points