Class CS and class Key are both children of class Action.
Class Key constructor takes an Action* parameter.
Key instantiation on line 34 compiles.
Key instantiation on line 37 does not compile; why is that?
#include <iostream>
class Action
{
public:
virtualvoid press()=0;
};
class Shift: public Action
{
public:
void press() { std::cout << " press "; }
};
class SC: public Action
{
private:
constchar scancode;
public:
SC(constchar sc): scancode(sc) { }
void press() { std::cout << scancode; }
};
class Key
{
private:
Action* const ptrAction;
public:
Key(Action* const a): ptrAction(a) { }
void press() { ptrAction->press(); }
};
SC sc_1('1');
Key key_1(&sc_1); //this line compiles
Shift shift();
Key key_shift(&shift); //error: no matching function for call to 'Key::Key(Shift (*)())'
// Key key_shift(&shift);
// ^
int main()
{
key_1.press();
//key_shift.press();
}
C:\demo_MinGW>g++ aggregation.cpp
aggregation.cpp:37:21: error: no matching function for call to 'Key::Key(Shift (
*)())'
Key key_shift(&shift);
^
aggregation.cpp:37:21: note: candidates are:
aggregation.cpp:29:3: note: Key::Key(Action*)
Key(Action* const a): ptrAction(a) { }
^
aggregation.cpp:29:3: note: no known conversion for argument 1 from 'Shift (*)
()' to 'Action*'
aggregation.cpp:24:7: note: Key::Key(const Key&)
class Key
^
aggregation.cpp:24:7: note: no known conversion for argument 1 from 'Shift (*)
()' to 'const Key&'
In this context, shift is a function that takes no parameters and returns a Shift object.
Remove the parenthesis after it (from Shift shift(); to Shift shift;).