【发布时间】:2012-12-04 02:35:52
【问题描述】:
我创建了一个头文件 Persona.h,在 Persona.cc 中我正在初始化类的所有变量和函数,为什么我不能从 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;
};
它给了我一个错误:
invalid use of non-static data member 'Persona::Nome'
我不知道该怎么做,你能吗?
谢谢。
【问题讨论】:
-
标记为
private,你期待什么? -
请获取a book from here。如果你在没有掌握基本知识的情况下继续下去,你只会严重伤害自己。
-
如果没有更多上下文很难判断问题出在哪里,但请尝试将方法声明更改为
Persona::Nasci。否则编译器无法知道该函数是Persona的方法。 -
@DomeWTF:“......来自类函数”?您没有“类功能”。你有一个完全独立的、不相关的、独立的函数
Nasci,它与类绝对无关。这是第一个问题。 -
#ifndef Persona_h #include "Persona.h" #endif #ifndef STD_LIB_H #include <string> #endif不不不。这个逻辑进入 in 标头,包含器不应该跟踪这些东西。可以根据需要多次包含<string>,不要像那样混乱你的代码。而且你的头文件应该把自己包裹在包含保护中,而包含器只是简单地包含它。
标签: c++ class non-static