"Name of the variable" was not declared in this scope
The error, specifically tells you that the variable called Nombredelavariable
does not exist in the scope in which it is being used. I know it's a truism but it's important to stress it to understand the error.
Let's go then, step by step. I will use the variable m_nomLocal
as an example, but it is applicable to the others.
... in this scope.
In this area. What do you mean? Let's see the use of m_nomLocal
:
CCadena GetLocal()
{
return m_nomLocal; // m_nomLocal not declared in this scope
}
We know that m_nomLocal
is part of the class CPartit
and we know that GetLocal
is a member function of that class, so this code should work right?.
The truth is that no. Because the GetLocal
function that we have just seen is not part of CPartit
but it is free. In other words, the scope of GetLocal
is the global scope. If we want it to be part of the scope of the class CPartit
, we must prepend the scope of the class to the name of the function:
CCadena CPartit::GetLocal()
{
return m_nomLocal;
}
With the previous modification, we added the function GetLocal
to the scope of the class CPartit
.
... was not declared ...
It has not been declared. What do you mean? If we have a function of the global scope:
CCadena GetLocal()
{
return m_nomLocal;
}
This can only access functions of the same scope or less restrictive areas. Given that the global scope is the least restrictive scope of all, it will only be able to access variables from the global scope.
The variable m_nomLocal
does not belong to the global scope but belongs to the scope of CPartit
, in other words: the full name of the variable would be CPartit::m_nomLocal
and consequently does not exist (it has not been declared) no variable with the name m_nomLocal
in the global scope.
Another example
// Variable en ambito global.
CCadena m_nomLocal;
// Función de ambito global.
CCadena GetLocal()
{
// Acceso a la variable global 'm_nomLocal'.
return m_nomLocal;
}
// Función en ambito 'CPartit'.
CCadena CPartit::GetLocal()
{
// Acceso a la 'm_nomLocal' del ámbito 'CPartit'.
return m_nomLocal;
}
We see that although we have two variables with the same name ( m_nomLocal
) each function accesses different variables because each of the functions belongs to a different scope, you can see the working example here .
As " curiosity ", if you would like to access m_nomLocal
global from the scope of CPartit
you should refer to the variable as ::m_nomLocal
.