gpt4 book ai didi

c++ - 使用私有(private)的成员变量 c++

转载 作者:行者123 更新时间:2023-11-28 00:24:26 25 4
gpt4 key购买 nike

我添加了所有代码。

我面临的确切问题是,当我在 game.h 文件中将成员变量设置为 private 时,我在 game.cpp 文件中收到一个错误,上面写着 n p h都是game.h的私有(private)成员。在 Xcode 中。

但是当我从命令行编译程序时,它编译得很好,没有错误。

我想了解我是否做错了什么,或者我这样做是否符合标准?

这是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;
}

游戏.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'; }

};

最佳答案

您犯了几个错误 - 打字错误是您遇到问题的原因。不允许该函数访问这些成员,因为它不是该类的友元。 friend 是(正确)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; }

你的操纵者改变了错误的变量!您更改传递给函数的那些而不是该类的成员...。但是,为三个私有(private)成员添加直接操纵器是非常值得怀疑的。您必须将您的类视为一些抽象对象,并定义在该对象上工作的运算符。如果您只是直接访问它的所有成员,那么使用带有私有(private)成员的类背后的面向对象思想就所剩无几了(尽管这仍然比将它们公开要好十倍!)

最后,只是一个编码风格提示。自定义类(即 Game 类)使用 CamelCase 名称是常见的做法,您最好为私有(private)成员添加前缀以将它们与函数参数区分开来。人们经常使用前缀 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) { }

等等

关于c++ - 使用私有(private)的成员变量 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25799132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com