I am learning C++ and the error that appears in Eclipse is:
"VariableName" was not declared in this scope
In the document.h I have the variable declared m_nomLocal
, so I don't know what it can refer to.
In the .h file I declare some functions and the variables:
#ifndef PARTIT_H
#define PARTIT_H
#include <iostream>
using namespace std;
#include "cadena.h"
class CPartit {
public:
CCadena GetLocal();
CCadena GetVisitant();
int GetPuntsLocal();
int GetPuntsVisitant();
private:
int m_puntsLocal;
int m_puntsVisitant;
int m_puntsTotal;
CCadena m_nomLocal;
CCadena m_nomVisitant;
int partit;
};
#endif
And in the .cpp I define them such that:
#include <iomanip>
using namespace std;
#include "partit.h"
CCadena GetLocal()
{
return m_nomLocal;
}
CCadena GetVisitant()
{
return m_nomVisitant;
}
int GetPuntsLocal()
{
return m_puntsLocal;
}
int GetPuntsVisitant()
{
return m_puntsVisitant;
}
The error specifically tells you that the called variable
Nombredelavariable
does not exist in the scope in which it is being used. I know it's a truism but it's important to point it out to understand the error.So let's go 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 ambit. What do you mean? Let's see the use of
m_nomLocal
:We know it
m_nomLocal
's part of the classCPartit
and we know itGetLocal
's a member function of that class, so this code should work right?The truth is that no. Because the function
GetLocal
that we have just seen is not part ofCPartit
it, it is free. In other words the scope ofGetLocal
is the global scope. If we want it to be part of the class scope,CPartit
we must prepend the class scope to the function name:With the above modification, we have added the function
GetLocal
to the class scopeCPartit
....was not declared...
It has not been declared. What do you mean? If we have a function of the global scope:
This will only be able to access functions of the same scope or less restrictive scopes. Since the global scope is the least restrictive of all scopes, you will only be able to access variables in the global scope.
The variable
m_nomLocal
does not belong to the global scope but rather belongs to the scope ofCPartit
, in other words: the full name of the variable would beCPartit::m_nomLocal
and consequently there is no variable with the namem_nomLocal
in the global scope (it has not been declared).Another example
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 example in operation aquí.As a " curiosity ", if you wanted to access
m_nomLocal
global from the scope ofCPartit
you should refer to the variable as::m_nomLocal
.Imagine that you have to modify the code of a colleague of yours and you find a function such that:
What information can you get from this code? The conclusions I would come to is that it
func
is a loose function (it does not belong to any class) and that itvariable
must be a static variable (since the function does not belong to any class).Well, exactly the same thing will happen to the compiler and when searching
variable
the list of static variables it will not find it and will show you the corresponding error.For the compiler to know that it
func
is a member function of a class, you must indicate it by adding the name of said class:Now the compiler will understand that it
func
is a member function ofMiClase
, and itvariable
can now be a member ofMiClase
or (if it doesn't haveMiClase
any named variables) a static variable.In your case the functions should look like this:
I know what happens to you my friend! It's a bug that has happened to me from time to time. you say in partit.h I define the variable
m_nomLocal
. and when I use it in partit.cpp it tells me that it is not defined so what is happening?Well it happens that when you call the variable
m_nomLocal
from the partit.cpp file you assume that the compiler understands that it is part of the classCPartit
because you have included the partit.h file in the header, but it is not. you must tell it clearly that the function belongs to the classCPartit
it is similar to what happens with the functions belonging to
std
likestd::cout
orstd::endl
etc.. and that for one to avoid doing that throughout the program write aboveusing namespace std
I hope that solves your problem