调用函数时的 C++ Null 输出

C++ Null output when a function is called

下面是我的主程序的一段代码

我的H档

class Person{
public:
     std::string name;
     int rangeStance;
     int initialStance;

Person(std::string name, int rangeStance, int initialStance){
     name = name;
     rangeStance = rangeStance;
     initialStance = initialStance;

     setName(getName());
     setRangestance(getRangeStance());
     setinitalStance(getRangeStance());
}
Person();
    void setName(std::string name);
    void setRangestance(int range);
    void setinitalStance(int stance);

    std::string getName();
    int getRangeStance();
    int getinitalStance();
    double impact(int rangeStance, int  initalStance);

};

class Leader: public Person {

    public:
     int popularity;
     int totalcountryVotes;

     Leader(std::string name, int rangeStance, int initialStance,int popularity, int totalcountryVotes)
     :Person(name,  rangeStance, initialStance), popularity(popularity), totalcountryVotes(totalcountryVotes){

          popularity = popularity;
          totalcountryVotes = totalcountryVotes;
          setPopularity(getPopularity());
          setTotalcountryVotes(getTotalcountryVotes());
     }

     Leader();   
     void setPopularity(int popularity);
     void setTotalcountryVotes(int totalcountryVotes);
     int getPopularity(); 
     int getTotalcountryVotes();   
};

主cpp文件中对应的函数

Person::Person() {
}

      void Person::setName(string Name)
        {
           name = Name;
        } 

        string Person::getName() {
              return name;
        }


        void Person::setRangestance(int Range)
        {

      rangeStance = Range;

        }

        int Person::getRangeStance() {
              return rangeStance;
        }

          void Person::setinitalStance(int stance)
        {

            initialStance = stance;                                            

        } 

        int Person::getinitalStance() {
              return initialStance;
        }



Leader::Leader() {

}

    void Leader::setPopularity(int popularity) {

          popularity = popularity;

     }


    void Leader::setTotalcountryVotes(int totalcountryVotes) {
          totalcountryVotes = totalcountryVotes;
     } 


     int Leader::getPopularity() {

           return popularity;
     }


    int Leader::getTotalcountryVotes() {
          return totalcountryVotes;
    }

在 main 中适当地调用了所需的函数

int main(int argc, char* argv[]) {

Leader labourLeader("George Lopez",100,50,50, 75);//sets record for the labour party leader

cout << "--Party Leader--" << endl;
cout << labourLeader.getName() << endl;

return 0;
}

然而,当编译这段代码时,在应该打印出来的地方没有返回任何结果 "George Lopez"。我相当 "noob" 使用 c++,我是在正确使用我的构造函数还是应该在我的 h 文件中删除它?谢谢你。

Person(std::string name, int rangeStance, int initialStance) {
    name = name;

这里发生的事情是它只是用自身覆盖参数,而不是将其复制到成员变量。那是因为此时非限定名称的名称查找规则更喜欢参数而不是成员变量。这意味着成员变量被保留在其构造状态,一个空字符串。


有几种方法可以解决这个问题。第一种是简单地给它们起不同的名字,这样就不会产生歧义,比如常用的方法是在成员变量前加上m_前缀。这样,语句就变得更明确了:

m_name = name;

另一种选择是明确关于您要分配给的对象,这样它就不再是不合格的:

this->name = name;

第三种是使用初始化列表,其中规则略有不同,因为它使用括号外的成员变量并在括号内内进行正常的非限定查找:

Person(std::string name, int rangeStance, int initialStance)
    : name(name)
    , rangeStance(rangeStance)
    , initialStance(initialStance)
    //      ^             ^
    //      |             |
    //      |             +- normal lookup, passed-in parameter.
    //      +--------------- member variable.
{
};

并且不需要在构造函数中包含所有其他语句,例如 setName(getName()),因为您已经已经设置了名称。

此代码中有几处错误

Person(std::string name, int rangeStance, int initialStance){
    name = name;
    rangeStance = rangeStance;
    initialStance = initialStance;
    setName(getName());
    setRangestance(getRangeStance());
    setinitalStance(getRangeStance());
}

首先,没有必要调用 setter 和进行赋值,所以让我们放弃这些,留下

Person(std::string name, int rangeStance, int initialStance){
    name = name;
    rangeStance = rangeStance;
    initialStance = initialStance;
}

现在想想 name = name 做了什么。你觉得这很好奇吗?它把参数name赋值给参数name!也叫name的成员变量完全没有变化。这种一个名字隐藏另一个相似名字的情况叫做shadowing.