使用私有c++的成员变量
Using Member Variables That Are Private c++
我添加了所有的代码。
我面临的确切问题是,当我在game.h
文件中创建成员变量private
时,我在game.cpp文件中得到一个错误,说n p h
都是Xcode
中game.h的私有成员。
但当我从命令行编译程序时,它编译得很好,没有错误。
我试图理解我是做错了什么,还是我这样做符合标准?
这是main.cpp
#include "game.h"
int main() {
game g("Female", "Magic", true, 21, 5, 120);
std::cout << "These are the things every game needs to be a game" << 'n';
std::cout << g << 'n';
return 0;
}
game.cpp
#include <iostream>
#include "game.h"
std::ostream& operator<<(std::ostream& s, const game& g) {
return s << &g.n << ' ' << &g.p << ' ' << &g.h;
}
这是我的复合类
#include <iostream>
#include "npc.h"
#include "pc.h"
#include "health.h"
class game {
private:
npc n;
pc p;
health h;
public:
game(const npc& init_n, const pc& init_p, const health& init_h):
n(init_n),
p(init_p),
h(init_h)
{}
game(std::string gen, std::string abil, bool use, int lvl, int h, int arm) :
n(gen, abil),
p(use, lvl),
h(h, arm)
{
}
friend std::ostream& operator<<(std::ostream& s, const game& g) {
g.n.output(s);
g.p.output(s);
g.h.output(s);
return s;
}
npc get_n() { return n; }
pc get_p() { return p; }
health get_h() { return h; }
void set_n(npc init_n) { n = init_n; }
void set_p(pc init_p) { p = init_p ; }
void set_h(health init_h) { h = init_h; }
};
这是一个等级
#include <iostream>
class health {
private:
int hp;
int armor;
public:
health(int init_hp, int init_armor) :
hp(init_hp),
armor(init_armor)
{
}
public:
void output(std::ostream& s) const { s << "Characters have this amount of hit points "<< hp << " and an armor rating of " << armor << "n"; }
};
这是一个等级
class pc {
private:
bool user;
int level;
public:
pc(bool init_user, int init_level) :
user(init_user),
level(init_level)
{
}
public:
void output(std::ostream& s) const { s << "A player character has at least "<< user << " user and a level of " << level << 'n'; }
};
这是一个等级
#include <iostream>
class npc {
private:
std::string gender;
std::string ability;
public:
npc(std::string init_gender, std::string init_ability) :
gender(init_gender),
ability(init_ability)
{
}
public:
void output(std::ostream& s) const { s << "A non player character has a gender of "<< gender << " and an ability of " << ability << 'n'; }
};
您犯了几个错误-拼写错误是造成问题的原因。函数不允许访问这些成员,因为它不是类的朋友。朋友是(正确的)std::ostream& operator<<(std::ostream& s, const game& g)
在定义函数std::ostream& operator<<(std::ostream& s, const game g)
时,请注意缺少的"与"符号。
此外,您的访问器应该是const,并返回const引用。即
npc const& get_n() const { return n; }
pc const& get_p() const { return p; }
health const& get_h() const { return h; }
你的操纵器改变了错误的变量!您可以更改传递给函数的,而不是该类的成员。。。。然而,为三个私人成员添加直接操纵者是非常值得怀疑的。您必须将类视为某个抽象对象,并定义对该对象有效的运算符。如果你只允许直接访问它的所有成员,那么使用带有私人成员的类就几乎没有面向对象的想法了(尽管如此,这仍然比公开它们好十倍!)
最后,只是一个编码风格提示。通常的做法是将CamelCase名称用于自定义类(即类Game),最好为您的私有成员添加前缀,以将它们与函数参数区分开来。人们经常使用前缀m_
。你真的应该使用完整的英语单词(不要缩写,更不用说单个字符了)。
这会把你的代码变成,比如。。。
class Game {
private:
Npc m_npc;
Pc m_pc;
Health m_health;
public:
Game(Npc const& npc, Pc const& pc, Health const& health) :
m_npc(npc), m_pc(pc), m_health(health) { }
等等。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 将包含C样式数组的对象初始化为成员变量(C++)
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 在循环中按顺序遍历成员变量
- c++类声明时,相同的例程,不同的成员变量类型
- 如何从另一个文件继承私有成员变量和公共函数
- 在C++类中,是否必须初始化作为数组的成员变量
- 如何从子成员函数修改父公共成员变量
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 从私有成员变量的成员方法返回unique_ptr
- 在派生类中使用基类的私有成员变量的最佳方法
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++:是否可以使用非静态成员变量模板?
- 打印所有继承的类成员变量和方法
- 如何在复杂继承中访问静态成员变量
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 成员变量与函数概念检查