Error in basic code c ++. "Name of the variable" was not declared in this scope

4

I am learning C ++ and the error that appears in Eclipse is:

  

"Name of the variable" was not declared in this scope

In the document.h I have declared the variable m_nomLocal , so I do not know what you can refer to.

In the .h file I declare some functions and 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 as:

#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;
}
    
asked by ElPatrón 12.03.2017 в 21:39
source

3 answers

4
  

"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 .

    
answered by 13.03.2017 / 09:27
source
2

Imagine that you have to modify the code of a partner of yours and you find yourself with a function such that:

int func()
{
  return variable;
}

What information can you get out of this code? The conclusions I would arrive at is that func is a loose function (does not belong to any class) and that variable must be a static variable (since the function does not belong to any class).

Well, the exact same thing will happen to the compiler and when searching for variable in the list of static variables it will not find it and it will show you the corresponding error.

In order for the compiler to know that func is a member function of a class, you must indicate it by adding the name of that class:

int MiClase::func()
{
  return variable;
}

Now the compiler will understand that func is a member function of MiClase , and now variable can be a member of MiClase or (if MiClase does not have any variable named like this) a static variable.

In your case, the functions should look like this:

CCadena CPartit::GetLocal()
{
  return m_nomLocal;
}
    
answered by 13.03.2017 в 09:26
1

I know what happens to you, my friend! It is a mistake 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 file partit.cpp you assume that the compiler understands that it is part of the class CPartit because you have included the file > partit.h in the header, but it's not like that. you must tell him clearly that the function belongs to the class CPartit

CCadena CPartit::GetLocal()
{
return m_nomLocal;
}

is similar to what happens with the functions belonging to std like std::cout or std::endl etc .. and that for one to avoid doing that during the whole program write up using namespace std I hope that resolves your problem

    
answered by 13.03.2017 в 08:32