Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Provides no initializer for reference member..."

Tags:

c++

object

oop

After some google search I cannot find an answer to this question. How do I initialize it, and why do I need to?

#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();
};
like image 650
Byron Mihailides Avatar asked May 06 '15 06:05

Byron Mihailides


1 Answers

Instead of:

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

fModel = aModel;
fView = aView;
}

Use

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

fModel and fView are reference member. Different instances of CalculatorController can share the same instances fModel and fView this way, without using nasty pointer.

Reference member have to be initialized on creation. My second code block show how to.

like image 169
Martin Schlott Avatar answered Oct 01 '22 05:10

Martin Schlott