经过一些谷歌搜索,我找不到这个问题的答案。我如何初始化它,为什么需要这样做?

#include "CalculatorController.h"


CalculatorController::CalculatorController(SimpleCalculator& aModel, ICalculatorView& aView)
{\\(this is the bracket informing me of the error)

fModel = aModel;
fView = aView;
}

header :
#pragma once

#include  "ICalculatorView.h"
#include "SimpleCalculator.h"

class CalculatorController
{
private:
 SimpleCalculator& fModel;
 ICalculatorView& fView;
public:
 CalculatorController(SimpleCalculator& aModel, ICalculatorView& aView);

 void run();
 ~CalculatorController();
};

最佳答案

代替:

CalculatorController::CalculatorController(SimpleCalculator& aModel, ICalculatorView& aView)
{\\(this is the bracket informing me of the error)

fModel = aModel;
fView = aView;
}


CalculatorController::CalculatorController(SimpleCalculator& aModel, ICalculatorView& aView)
 : fModel(aModel),fView(aView)
{
}

fModel和fView是引用成员。这样,无需使用讨厌的指针,CalculatorController的不同实例可以共享相同的实例fModel和fView。

引用成员必须在创建时进行初始化。我的第二个代码块显示了如何操作。

关于c++ - "Provides no initializer for reference member...",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30069384/

10-11 15:41