• 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

Please log in or register to answer this question.

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

0 votes
1 answer 35 views
Problem: I am working on functions in C++. I am facing the above mentioned error while I am trying to run the following program: #include <iostream> using namespace std; int main() { cout<<func(4); } int func(int number) { return number; } I have no idea about the error. Why this error is being triggered and how can I resolve this issue?
asked Nov 10, 2020 Code Learner 5.5k points
0 votes
1 answer 6 views
Problem: I'm just learning c++ (first day looking at it since I took a 1 week summer camp years ago) I was converting a program I'm working on in Java to C++: The errors were: SkewNormal.h: In function 'double getSkewNormal(double, double)' : SkewNormal. ... SkewNormal.h: In function 'double integrate(double, double, int, Evaluatable)': SkewNormal.h:41: error: 'sum' was not declared in this scope
asked Dec 15, 2020 Mashhoodch 5.6k points
0 votes
1 answer 17 views
Problem Hey, I Am trying to convert the string to an array by using stoi. But it&rsquo;s showing an error &lsquo;stoi&rsquo; was not declared in the scope. I have a standard library and a string as well. Can somebody help me to correct the code that i have been given in the next ... init; string hours0 = init.substr(0,2); int hours = stoi(hours0); cout << hours << endl; system("pause"); return 0;
asked Dec 24, 2020 chris jordan 2.2k points
0 votes
1 answer 3 views
Problem: I have a program which prompts me the error in VS2010, in debug : Error: Stack around the variable 'x' was corrupted This gives me the function where a stack overflow likely occurs, but I can't visually see where the problem is. Is there a general way to debug this error with VS2010? Would it be possible to indentify which write operation is overwritting the incorrect stack memory? thanks
asked Dec 22, 2020 Mashhoodch 5.6k points
0 votes
1 answer 7 views
Problem: What I want to do is just to define a variable in a header file and use it on two different cpp files without redefinition that variable each time I include that header Here is how I tried : Variables.h #ifndef VARIABLES_H // header guards #define VARIABLES_H static bool bShouldRegister; #endif
asked Dec 15, 2020 Mashhoodch 5.6k points
1 vote
1 answer 51 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, 2020 Gavin 15.3k points
0 votes
1 answer 22 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, 2020 game 4.6k points
0 votes
1 answer 10 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, 2020 game 4.6k points
1 vote
1 answer 170 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, 2020 adamSw 11.3k points
0 votes
1 answer 7 views
Problem Hey, want to ask you about my problem regarding a file passing to a function. I was trying to create a file which will ask to user for its path then passing it as an argument to a new input/ output then passing that file as a reference to update file ... ::ifstream OutputFile (FilePath);     std::ofstream InputFile (FilePath);          ModifyFile(OutputFile, InputFile);     return 0; }
asked Jan 12 chris jordan 2.2k points