• Register
1 vote
72 views

Problem:

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.

Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

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:

#include<iostream> 
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,260 points

Related questions

0 votes
1 answer 76 views
76 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 9.8k points
1 vote
1 answer 12 views
12 views
Problem: Please help me. How can I solve it? variable was not declared in this scope
asked Mar 13 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 6 views
6 views
Problem: Any ideas on what I need to do to fix this? variable not declared in this scope
asked Mar 13 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 11 views
11 views
Problem Anyones has any idea what is the instance variable in c++, This keyword is new for me, If anyones do have any knowledge, can you share with me.
asked Apr 5 Subhendu 4.5k points
0 votes
1 answer 6 views
6 views
Problem: Hi there! I want to know that is there any way to declare a function that could take a variable number of arguments of the same type? I want something like follows: function(int arg1, arg2, &hellip;.) {} If there is such a mechanism, how can it be used? Please give an example. Thanks!
asked Mar 28 Code Learner 9.8k points
0 votes
1 answer 6 views
6 views
Problem: How do I declared c++ string in this scope?
asked Apr 17 sumaiya simi 43.9k points
0 votes
1 answer 11 views
11 views
Problem: Hi there! I am new to C++ and I am trying to convert some Java code to C++. I have worked with Java earlier. Following is the Java code: public class Main { public static void main(String[] args) { int first_Number = 3; int second_Number = 5; ... scope Divide(first_Number, second_Number); ^~~~~~ I have no idea why this is happening. Can anyone please help me sort out these errors? Thanks!
asked Mar 17 Code Learner 9.8k points
0 votes
1 answer 40 views
40 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 13k points
0 votes
1 answer 6 views
6 views
Problem: Please let me know any solution regarding this .. uselocale was not declared in this scope
asked Apr 20 Ifra 34.7k points
0 votes
1 answer 5 views
5 views
Problem: So I was writing this simple program to calculate the day of any date using the Gaussian algorithm. It's a very simple program and what's even more puzzling is the output. :25: error: ‘c’ was not declared in this scope
asked Apr 19 ummesalma 29.2k points