作为类成员初始化的引用

发布于 2024-12-18 00:39:40 字数 928 浏览 0 评论 0原文

我想通过将此类引用作为参数传递给构造函数来初始化包含对另一个类的引用的类的属性。但是我收到错误:

“'TaxSquare::bank' 必须在构造函数基/成员初始值设定项列表中初始化”。 下面的类代码有什么问题?

#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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘香 2024-12-25 00:39:40

您正在尝试分配给 bank,而不是初始化它:

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

bank 是一个引用,因此必须对其进行初始化。您可以通过将其放入初始值设定项列表中来完成此操作:

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

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 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)
{}
听闻余生 2024-12-25 00:39:40

“'TaxSquare::bank' 必须在构造函数基/成员初始值设定项列表中初始化”。下面的类代码有什么问题?

错误在于 TaxSquare::bank 没有在构造函数基/成员初始化列表中初始化,正如它所说的那样。

“构造函数基/成员初始化列表”是相关构造函数的初始化列表,TaxSquare::TaxSquare(int, int, Bank&)。您已经在使用它来初始化底座 (Square)。您必须使用它来初始化 bank 成员,因为它是引用类型。初始化列表中未指定的内容将被默认初始化,并且引用没有默认初始化,因为它们必须始终引用某些内容,并且没有默认的内容可供它们引用。

老实说,我发现在 C++ 中使用数据成员的引用在 99% 的情况下麻烦多于其价值。您可能最好使用智能指针,甚至是原始指针。但是您应该仍然使用初始化列表来初始化它,即使您可以不使用初始化列表。实际上,taxAmount 也是如此。

// TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID)
// That thing after the colon is the initialization list:      ^^^^^^^^^^^^
// So add the other members to it, and then notice that there is nothing left
// for the constructor body to do:
TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : 
Square(anID), taxAmount(amount), bank(theBank) {}

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

What is wrong is that TaxSquare::bank is not being initialized in the constructor base/member initialization list, exactly as it says.

"The constructor base/member initialization list" is the initialization list for the constructor in question, TaxSquare::TaxSquare(int, int, Bank&). You're already using it to initialize the base (Square). You must use it to initialize the bank member, because it is of a reference type. Things not specified in the initialization list get default-initialized, and there is no default-initialization for references, because they must always reference something, and there is no default something for them to reference.

Honestly, I find that using references for data members in C++ is more trouble than it's worth, 99% of the time. You're probably better off with a smart pointer, or even a raw one. But you should still initialize that with the initialization list, even if you could get away without. Same goes for the taxAmount, really.

// TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID)
// That thing after the colon is the initialization list:      ^^^^^^^^^^^^
// So add the other members to it, and then notice that there is nothing left
// for the constructor body to do:
TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : 
Square(anID), taxAmount(amount), bank(theBank) {}
命比纸薄 2024-12-25 00:39:40

错误是您尝试通过未初始化的引用进行分配:无法分配 C++ 引用 - 而是分配它所引用的对象 - 因此,如果它是成员,则必须在初始值设定项列表中对其进行初始化(就像编译器一样)说)。

The error is you're trying to assign through an uninitialized reference: a C++ reference cannot be assigned - the object it refers to is assigned instead - and so, if it's a member, it must be initialized in the initializer list (like the compiler says).

攒一口袋星星 2024-12-25 00:39:40

银行=银行;
该语句意味着您将 obj1 分配给 obj2 并且它将调用赋值运算符,这是错误的,因为 Bank 是引用类型,它必须按如下所述进行初始化

TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID )、银行(theBank){}

bank = theBank;
this statement means you are assigning obj1 to obj2 and it will call Assignment operator which is wrong as bank is of type reference it must be intialized as mentioned below

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文