繁体   English   中英

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

[英]Using Member Variables That Are Private c++

我添加了所有代码。

我面临的确切问题是,当我在game.h文件中将成员变量game.h private时,我在game.h文件中得到了一个错误,提示nph都是nph的私有成员。 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;
}

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) 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) { }

等等

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM