简体   繁体   English

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

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

I added all my code. 我添加了所有代码。

The exact problem I am facing is when I make the member variables private in the game.h file I get an error in the game.cpp file that says nph are all private members of game.h. 我面临的确切问题是,当我在game.h文件中将成员变量game.h private时,我在game.h文件中得到了一个错误,提示nph都是nph的私有成员。 in Xcode . Xcode

But when I compile the program from the command line it compiles fine with no errors. 但是,当我从命令行编译程序时,它可以正常编译且没有错误。

I am trying to understand if I am doing something wrong or is this up to standards the way I am doing this? 我想了解我做错了什么,还是这符合我的标准?

This is main.cpp 这是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 game.cpp

#include <iostream>

#include "game.h"

std::ostream& operator<<(std::ostream& s, const game& g) {
    return s << &g.n << ' ' << &g.p  << ' ' <<  &g.h;
}

This is my composite class 这是我的综合课

#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; }
};

Here is a class 这是一堂课

#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";  }

    };

Here is a class 这是一堂课

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';  }

};

Here is a class 这是一堂课

#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';  }

};

You made several errors - a typo is the reason for your problem. 您犯了几个错误-错字是造成问题的原因。 The function is not allowed to access those members because it is not a friend of the class. 该函数不是该类的朋友,因此不允许访问这些成员。 The friend is (correctly) std::ostream& operator<<(std::ostream& s, const game& g) while you defined a function std::ostream& operator<<(std::ostream& s, const game g) , note the missing ampersand. 当您定义函数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) ,请注意缺少“&”号。

Also, your accessors should be const, and return a const reference. 另外,您的访问器应为const,并返回const引用。 Ie,

 npc const& get_n() const { return n; }
 pc const& get_p() const { return p; }
 health const& get_h() const { return h; }

Your manipulators change the wrong variables! 您的操纵器更改了错误的变量! You change the ones passed to the function instead of the members of that class.... However, it is highly questionable that you add direct manipulators for the three private members. 您可以更改传递给函数的函数,而不是该类的成员。...但是,为三个私有成员添加直接操纵器却是一个很大的问题。 You must view your class as some abstract object and define operators that work on that object. 您必须将类视为某些抽象对象,并定义在该对象上工作的运算符。 If you just give direct access to all it's members than there is little left of the object orientation idea behind using a class with private members (this would still be ten times better than making them public though!) 如果您只是直接访问所有成员,则使用面向私有成员的类所留下的面向对象的想法就几乎没有了(尽管这比公开它们要好十倍!)

Finally, just a coding style hint. 最后,只是一个编码样式提示。 It is common practice to use CamelCase names for custom classes (ie, class Game), and you're better of adding a prefix to your private members to distinguish them from function parameters. 通常将CamelCase名称用于自定义类(即Game类),并且最好在私有成员中添加前缀以将其与函数参数区分开。 Often people use the prefix m_ . 人们通常使用前缀m_ You really should use complete english words too (not abbreviations, let alone single characters). 您确实也应该使用完整的英语单词(不要缩写,更不用说单个字符了)。

This would turn your code into, say... 这将使您的代码变成...

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

etc. 等等

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

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