-1

I have class A with constructor A(int a)

A.h

calss A{
  public :  A(int a);
}

A.cpp

#include "a.h"
#include <iostream>
A::A(int a)
{
    std::cout<<a<<end;
}

I need to initialize this class from another class B,

I tried

B.h

class B{
   public :  B();
            A tmp;
            //A tmp(4); //this  giving syntax error          
} 

and

B.cpp

 B(){
     tmp = A(4);
}

But I am getting compiler error "error: no matching function for call to 'A::A()'"

Haris
  • 13,645
  • 12
  • 90
  • 121
  • 1
    (1) Post the complete error message. (2) Constructors don't have return types, so remove the `void`. – Dietrich Epp Dec 31 '14 at 11:09
  • 1
    Constructors can not have return type. Not even void. – CreativeMind Dec 31 '14 at 11:09
  • Possible duplicate of [In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?](http://stackoverflow.com/questions/4589237/in-this-specific-case-is-there-a-difference-between-using-a-member-initializer) – πάντα ῥεῖ Dec 31 '14 at 11:22

5 Answers5

3

The problem is that A doesn't have a default constructor (because if you define another constructor and default one is not generated) and so A objects cannot be default constructed.

The solution is to use constructor initialization lists for every constructor of B where you call the appropriate constructor for tmp.

class B {
   A tmp;

public:
   B() : tmp(4) {}
};

If you can use c++11 and in every (or at least most of the) constructors of B you create tmp the same way you can use:

class B {
  A tmp{4};

public:
  B() {};
}
bolov
  • 72,283
  • 15
  • 145
  • 224
1

Constructors should not have a return type. Remove the void in the declaration and you should be good to go. Same goes for your class B.

Pzc
  • 88
  • 1
  • 3
  • 8
1

Constructors are special member functions without any return type.

//A tmp(4); //this  giving syntax error

This will for sure give you an error because you mentioned

 void A(int a) 

and this is not constructor; because its has return type (void). when you explicitly define parametrized constructor (like A(int a) )the default constructor gets vanished and for this statement

A tmp;

to compile you have to provide your own default constructor.

Ali Kazmi
  • 1,460
  • 9
  • 22
1

It would be great always to post complete error message.

Rather than getting "no matching function for call to" , I am sure that you will be getting below error:

error: no matching function for call to ‘A::A()’
note:                 A::A(int)

This clearly indicates that default constructor A::A() is missing from your code as you have provided your own constructor.

Suggestion: You have defined conversion constructor. It is good practice to use "explicit" keyword.

CreativeMind
  • 897
  • 6
  • 19
1

*.h file

class A
{
    public:
        A(int a);
};
class B
{
    public:
        B();
        A *tmpA;
};

*.cpp file

A::A(int a)
{
    MessageBox(NULL,L"",L"", MB_OK);
}
B::B()
{
    tmpA = new A(4);
}

usage

{
    B *b = new B();
}

is it correct for you?

pors
  • 25
  • 5