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.

DomeWTF
  • 2,342
  • 4
  • 33
  • 46
  • 1
    It's marked `private`, what do you expect? – Pubby Dec 04 '12 at 02:38
  • 2
    Plese get [a book from here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You are only going to badly hurt yourself if you continue without getting the basics right. – pmr Dec 04 '12 at 02: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`. – Yaniv Dec 04 '12 at 02: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. – AnT stands with Russia Dec 04 '12 at 02:54
  • `#ifndef Persona_h #include "Persona.h" #endif #ifndef STD_LIB_H #include #endif ` No no no. This logic goes *in* the header, the includer should not have to track this stuff. It's fine to include `` as many times as you wanted, don't clutter your code like that. And your header file should wrap itself in the include guards, and the includer simply includes it. – GManNickG Dec 04 '12 at 02:58
  • it gives me multiple definition of Persona::Nasci() – DomeWTF Dec 04 '12 at 03:22
  • 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`. – Yaniv Dec 04 '12 at 03:45
  • i changed the code to show how it really was on my pc since someone didn't get what it really was – DomeWTF Dec 04 '12 at 16:47
  • oh i see what you mean, sorry i just copied it wrong on the forum – DomeWTF Dec 04 '12 at 16:53

1 Answers1

2

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.

Jason
  • 31,834
  • 7
  • 59
  • 78
  • You'd also have to add the declaration for Nasci to the Persona class. – Jonathan Leffler Dec 04 '12 at 02:42
  • 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. – Jason Dec 04 '12 at 02:43