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

      作为类成员初始化的引用

      Reference as class member initialization(作为类成员初始化的引用)
        <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)
                {}
                

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                <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>
                            <tbody id='lmKoR'></tbody>