2

i created a header Persona.h and in Persona.cc i am initializing all the variables and functions of the class, why can't i access a variable from Persona.cc?

Persona.h

#ifndef STD_LIB_H
#include <iostream>
#endif
#ifndef STD_LIB_H
#include <string>
#endif

class Persona
{
    private:
        std::string Nome;

    public:
        Nasci(std::string);
};

Persona.cc

#ifndef Persona_h
#include "Persona.h"
#endif

#ifndef STD_LIB_H
#include <string>
#endif

void Persona::Nasci(std::string nome)
{
    // Nome della persona

    Nome = nome;
};

it gives me an error:

invalid use of non-static data member 'Persona::Nome'

i can't figure out what to do, can you?

Thank You.

9
  • 1
    It's marked private, what do you expect? Commented Dec 4, 2012 at 2:38
  • 2
    Plese get a book from here. You are only going to badly hurt yourself if you continue without getting the basics right. Commented Dec 4, 2012 at 2:39
  • It's hard to tell what the problem is without some more context, but try changing the method declaration to Persona::Nasci. Otherwise, the compiler has no way of knowing that the function is a method of Persona. Commented Dec 4, 2012 at 2:47
  • @DomeWTF: "... from class function"? You don't have a "class function". You have a completely independent, unrelated, standalone function Nasci, which has absolutely nothing to do with the class. That's the first problem. Commented Dec 4, 2012 at 2:54
  • 1
    You've substantially changed the code in your question after all these people commented, which isn't cool. The latest problem I see is that you're using different case in the .h and .cc files: nasci vs. Nasci. Commented Dec 4, 2012 at 3:45

1 Answer 1

3

I'm assuming Nasci is a method of Persona, and therefore your method definition should look like the following:

void Persona::Nasci(std::string nome)
{
    // Nome della persona
    Nome = nome;

    //...rest of the function
}

Otherwise, if Nasci is not a method or friend function of the Persona class-type, you cannot access a private data member of the Persona class inside the function body even though you've attempted to use namespace-scope resolution.

Typically when you see code that uses namespace scope resolution on a data-object or function inside another stand-alone function body like you've done, that data-member or function is a non-private static method or data-member of a specific class, and therefore is visible to other non-class functions. Of course there's a number of other uses for the scope resolution operator in C++, but just saying that in your case it would require Nome to be a non-private static data-member to avoid a compiler error. Of course using Nome in that way wouldn't fit your use scenario though, so what you're really wanting is the above code segment where you designate Nasci as a method of Persona.

Sign up to request clarification or add additional context in comments.

2 Comments

You'd also have to add the declaration for Nasci to the Persona class.
Since the OP didn't complete the declaration of the class (i.e., there's no closing bracket on the class declaraion), I'm assuming that Nasci is a method of the class they simply decided not to list for the sake of brevity rather than accidental omission. But yes, if they didn't include the declaration of Nasci in the class declaration, they would have to-do that as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.