1. <legend id='fCkU4'><style id='fCkU4'><dir id='fCkU4'><q id='fCkU4'></q></dir></style></legend>
    <tfoot id='fCkU4'></tfoot>
    • <bdo id='fCkU4'></bdo><ul id='fCkU4'></ul>
    <i id='fCkU4'><tr id='fCkU4'><dt id='fCkU4'><q id='fCkU4'><span id='fCkU4'><b id='fCkU4'><form id='fCkU4'><ins id='fCkU4'></ins><ul id='fCkU4'></ul><sub id='fCkU4'></sub></form><legend id='fCkU4'></legend><bdo id='fCkU4'><pre id='fCkU4'><center id='fCkU4'></center></pre></bdo></b><th id='fCkU4'></th></span></q></dt></tr></i><div id='fCkU4'><tfoot id='fCkU4'></tfoot><dl id='fCkU4'><fieldset id='fCkU4'></fieldset></dl></div>
  2. <small id='fCkU4'></small><noframes id='fCkU4'>

      作为类成员初始化的引用

      时间:2023-09-26
        <legend id='oK1MM'><style id='oK1MM'><dir id='oK1MM'><q id='oK1MM'></q></dir></style></legend>
          <tbody id='oK1MM'></tbody>
        1. <i id='oK1MM'><tr id='oK1MM'><dt id='oK1MM'><q id='oK1MM'><span id='oK1MM'><b id='oK1MM'><form id='oK1MM'><ins id='oK1MM'></ins><ul id='oK1MM'></ul><sub id='oK1MM'></sub></form><legend id='oK1MM'></legend><bdo id='oK1MM'><pre id='oK1MM'><center id='oK1MM'></center></pre></bdo></b><th id='oK1MM'></th></span></q></dt></tr></i><div id='oK1MM'><tfoot id='oK1MM'></tfoot><dl id='oK1MM'><fieldset id='oK1MM'></fieldset></dl></div>
        2. <tfoot id='oK1MM'></tfoot>

            1. <small id='oK1MM'></small><noframes id='oK1MM'>

              • <bdo id='oK1MM'></bdo><ul id='oK1MM'></ul>

              • 本文介绍了作为类成员初始化的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想通过将这样的引用作为参数传递给构造函数来初始化一个类的属性,该类保存对另一个类的引用.但是我收到一个错误:

                I want to initialize a property of a class that holds a reference to another class by passing such a reference as a parameter to the constructor. However I receive an error:

                'TaxSquare::bank' 必须在构造函数基类/成员初始化列表中初始化".以下类的代码有什么问题?

                "'TaxSquare::bank' must be initialized in constructor base/member initializer list". What is wrong in the following code of the classes?

                #ifndef TAXSQUARE_H
                #define TAXSQUARE_H
                #include "Square.h"
                
                class Bank;
                
                class TaxSquare : public Square
                {
                    public:
                      TaxSquare(int, int, Bank&);
                      virtual void process();
                
                    private:
                      int taxAmount;
                      Bank& bank;
                
                };
                #endif
                

                #include <iostream>
                #include "TaxSquare.h"
                #include "Player.h"
                #include "Bank.h"
                using namespace std;
                
                TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID)
                {
                  taxAmount = amount;
                  bank = theBank;
                }
                

                #ifndef BANK_H
                #define BANK_H
                
                class Bank
                {
                public:
                  Bank(int, int, int);
                  void getMoney(int);
                  void giveMoney(int);
                  void grantHouse();
                  void grantHotel();
                
                private:
                  int sumMoney;
                  int numOfHouses;
                  int numOfHotels;
                
                };
                
                #endif
                

                推荐答案

                您正在尝试分配给 bank,而不是对其进行初始化:

                You are attempting to assign to bank, not initialize it:

                TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID)
                {
                    // These are assignments
                    taxAmount = amount;
                    bank = theBank;
                }
                

                bank 是一个引用,因此必须对其进行初始化.你可以把它放在初始化列表中:

                bank is a reference, and therefore it must be initialized. You do so by putting it in the initializer list:

                TaxSquare::TaxSquare(int anID, int amount, Bank& theBank)
                : Square(anID), taxAmount(amount), bank(theBank)
                {}
                

                这篇关于作为类成员初始化的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:创建派生类对象时将参数传递给基类构造函数 下一篇:继承构造函数在 C++ 中有多大用处?

                相关文章

                <legend id='lmKoR'><style id='lmKoR'><dir id='lmKoR'><q id='lmKoR'></q></dir></style></legend>
                • <bdo id='lmKoR'></bdo><ul id='lmKoR'></ul>
                1. <i id='lmKoR'><tr id='lmKoR'><dt id='lmKoR'><q id='lmKoR'><span id='lmKoR'><b id='lmKoR'><form id='lmKoR'><ins id='lmKoR'></ins><ul id='lmKoR'></ul><sub id='lmKoR'></sub></form><legend id='lmKoR'></legend><bdo id='lmKoR'><pre id='lmKoR'><center id='lmKoR'></center></pre></bdo></b><th id='lmKoR'></th></span></q></dt></tr></i><div id='lmKoR'><tfoot id='lmKoR'></tfoot><dl id='lmKoR'><fieldset id='lmKoR'></fieldset></dl></div>
                2. <small id='lmKoR'></small><noframes id='lmKoR'>

                    <tfoot id='lmKoR'></tfoot>