No matching function for call to ‘Foo::Foo()’

From Cppsyntax

Jump to: navigation, search

The error

 no matching function for call to ‘Foo::Foo()’	

will happen if the function Foo::Foo() hasn't been defined yet, like if the #include file for Foo wasn't included. (See ‘Foo’ was not declared in this scope.)

Contents

Empty constructor implicitly needed

It can also happen if you do not define an empty constructor Foo(), but define a Foo instance variable somewhere:

WRONG

foo.h:

 class Foo {
   Foo(int);
 }

blort.h:

 class Blort 
   public:
     Foo aFoo;
 }

When Blort is created for the first time, it creates aFoo with an empty Foo() constructor. Since you don't have a Foo() constructor, the compiler realizes that it would never be able to make aFoo, so complains and won't let you do that.

You probably want to create a pointer to a Foo object for your instance variable:

MORE CORRECT

foo.h:

 class Foo {
   Foo(int);
 }

blort.h:

 class Blort 
   public:
     Foo *aFooPtr;
 }

Passing a constant to something expecting a ref

If you pass a constant to something expecting a ref, you can get into trouble. For example, in the example below, the function foo wants to be able to modify aBar; it can't do that if you pass it a numeric constant.

WRONG

 void foo(int &aBar) {
   // stuff
 }
 void boo() {
   foo(2);
 }

You can get around it if you assign the constant to a variable and then pass the variable.

RIGHT

 void foo(int &aBar) {
   // stuff
 }
 void boo() {
   int x = 2;
   foo(x);
 }

You should be nervous if you work around the problem by making a variable to hold the value, however. Things are frequently declared as references specifically so that the function can have beneficial side effects.

Missing * from malloc cast

You can get a variant of the above error, namely:

 no matching function for call to ‘Foo::Foo(void*)’

if you forget a * from a cast from a malloc:

WRONG

 Foo *fooPtr = (Foo)(malloc(16*sizeof(Foo));       // cast needs to be Foo*


RIGHT

 Foo *fooPtr = (Foo *)(malloc(16*sizeof(Foo));       // ahh, better!

Parent constructor not called

When you have class inheritance, and the parent class' constructor requires one or more arguments, e. g.:

 // foobar.h  
 
 class Foo
 {
   public:
     Foo(int a);
   private:
     int apples;
 }
 
 class Bar: public Foo
 {
   public:
     Bar(int a);
 }

this is WRONG:

 // foobar.cpp
 
 Foo::Foo(int a)
 {
     apples = a;
 }
 
 Bar::Bar(int a)
 {
     // ...
 }

and would return something in the lines of:

 foobar.cpp:4: error: no matching function for call to ‘Foo::Foo()’
 foobar.h:73: note: candidates are: Foo::Foo(int)
 foobar.h:71: note:                 Foo::Foo(const Foo&)

and the RIGHT thing that would help you is:

 // foobar.cpp
 
 Foo::Foo(int a)
 {
     apples = a;
 }
 
 Bar::Bar(int a) : Foo(a)
 {
     // ...
 }
pharmacie en ligne viagra professional cialis professional acheter viagra soft acheter viagra acheter propecia acheter cialis acheter tadalis acheter silagra acheter levitra acheter kamagra acheter caverta acheter kamagra oral jelly acheter cialis soft acheter kamagra soft
Personal tools