1

It might be a C++ beginner mistake but I'm really confused now by getting this compiler Error:

error: no matching function for call to 'B::B(A (*)())'
note: candidates are: B::B(A*)

I have written two class which look simplified like this:

//a very simple class A
class A
{
public:
    A()
    {
        //do some stuff in Constructor
    }
}

//another class, that stores a pointer to an object of A as a field
class B
{
private:
    A* _a;

public:
    //simply initialize the field in Constructor
    B(A* a) : _a(a) { }

    void doMagic()
    {
        //do something with _a
        _a->xyz();
    }
}

And what I am calling in my code is this:

A a();
B b(&a); //here the error appears

What I want is to create an object a and pass its pointer to object b. So that I have access to a in b's members.

It must be just something in the call of B b(&a); Perhaps you can find out what my Problem is very easily.

Sparkofska
  • 1,280
  • 2
  • 11
  • 34

3 Answers3

1

You're not creating an object of type A, instead, A a(); is a declaration of function, which takes no parameter, and the return type is A. So you're trying to pass a function pointer to the ctor of B, that's what compiler complains. You should change

A a();

to

A a;
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0
A a();

This is a function declaration. The rule is: Everything which can be a function declaration is a function declaration. This could be either constructor call or function declaration, tho it will never become a constructor call. Try this instead:

A a;
Melkon
  • 418
  • 3
  • 12
0
A a();

defines a function with the name a that returns an object of type A. You are passing that function to the constructor of type B.

Change the initialization of a to this so that you create an object of type a:

A a;
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49