使用私有c++的成员变量

Using Member Variables That Are Private c++

本文关键字:成员 变量 c++      更新时间:2023-10-16

我添加了所有的代码。

我面临的确切问题是,当我在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) { }

等等。