Constructor member initializations?

I have a class that has a constructor: Fighter::Fighter(FontHolder& fonts).

The World class, has a Fighter member: Fighter* World::m_fighter.

the World constructor looks like this
1
2
3
4
5
6
7
World::World(..., FontHolder& fonts,...)
   : ... ,
     m_fighter(fonts),
     ...,
{
     ...
}


but fonts in m_fighter(fonts) is underlined red and it says "Error: no suitable conversion from "FontHolder" to "Fighter*" exists. Why is it expecting a Fighter* there? If i do m_fighter(new Fighter(fonts)) the error goes away but i get problems in other places. Why cant i just do m_fighter(fonts)?
Why does Fighter* m_fighter expect a Fighter*?
m_fighter is a pointer. Pointer stores an address.

Constructor parameter fonts is a reference. Reference is not a pointer.

You could m_fighter( &fonts ),, but do you know the lifetime of the object that is passed as 'fonts' to constructor? Do you promise not to attempt delete of the m_fighter?
Because m_fighter isn't a Fighter object -- it's a pointer that may point to a Fighter object.

If that's not what you intended, remove the asterisk and just make it Fighter m_fighter; instead of Figher* m_figher;.
nano511 wrote:
Why is it expecting a Fighter* there? [...] Why cant i just do m_fighter(fonts)?


Because m_fighter is not a Fighter. It's a Fighter*.
lol my bad thanks i got it
Topic archived. No new replies allowed.