Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialise Constructor of a Nested Class in C++

I am getting problems in initializing the nested class constructor.

Here is my code:

#include <iostream>

using namespace std;

class a
{
public:
class b
{
    public:
    b(char str[45])
        {
        cout<<str;
        }

    }title;
}document;

int main()
{
    document.title("Hello World"); //Error in this line
    return 0;
}

The error I get is:

fun.cpp:21:30: error: no match for call to '(a::b)'
like image 628
sandbox Avatar asked Feb 21 '23 14:02

sandbox


1 Answers

You probably want something like:

class a
{
public:
    a():title(b("")) {}
    //....
};

This is because title is already a member of a, however you don't have a default constructor for it. Either write a default constructor or initialize it in the initialization list.

like image 127
Luchian Grigore Avatar answered Mar 04 '23 15:03

Luchian Grigore