Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 828279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:42:03+00:00 2026-05-15T03:42:03+00:00

I’m trying to instantiate a class within a class, so that the outer class

  • 0

I’m trying to instantiate a class within a class, so that the outer class contains the inner class.

This is my code:

#include <iostream>
#include <string>

class Inner {
    private: 
        std::string message;

    public:
        Inner(std::string m);
        void print() const;
};

Inner::Inner(std::string m) {
    message = m;
}

void Inner::print() const {
    std::cout << message << std::endl;
    std::cout << message << std::endl;
}

class Outer {
    private:
        std::string message;
        Inner in;

    public:
        Outer(std::string m);
        void print() const;
};

Outer::Outer(std::string m) {
    message = m;
}

void Outer::print() const {
    std::cout << message << std::endl;
}

int main() {
    Outer out("Hello world.");
    out.print();

    return 0;
}

“Inner in”, is my attempt at containing the inner within the outer, however, when I compile, i get an error that there is no matching function for call to Inner::Inner().
What have I done wrong?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:42:04+00:00Added an answer on May 15, 2026 at 3:42 am

    You need to use initialization lists to initialize class members:

    Inner::Inner(const std::string& m)
      : message(m)
    {
    }
    
    Outer::Outer(const std::string& m)
      : in(m)
    {
    }
    

    (Note that I passed the strings per const reference, which is better than passing them by value. See this answer for how to pass function arguments.)

    This way you can specify exactly which constructors should be called for class members.

    If you don’t specify a constructor, the default one will be called implicitly. Assigning to the object later will then invoke the assignment operator and override whatever the default constructor initialized the object to. That’s wasting performance at best.
    Since our Inner doesn’t have a default constructor (declaring any constructor prevents the compiler from defining a default constructor by itself), it cannot be called, so you need to specify the constructor taking a string explicitly.


    Edit: Note that, if you have more than one class member, they are all initialized that way, separated by commas:

    Outer::Outer(const std::string& m) : in1(m), in2(m), in3() {}
    

    Note that the order of initialization of class members is determined by their declaration order within the class definition, not by the order they appear in the initialization list. It’s best to not to rely on initialization order, since changing that in the class definition would then create a very subtle bug in the constructor’s definition. If you can’t avoid that, put a comment besides the class members declaration:

    class outer {
      public:
        outer(const inner& in) 
         : in_(in), rin_(in_) // depends on proper declaration order of in_ and rin_
        {}
      private:
        inner in_;   // declaration order matters here!1
        inner& rin_; // (see constructor for details)
    };
    

    Base class constructors are specified the same way, and they are initialized before class members, also in order of declaration in the base class list. Virtual base classes, however, are initialized before all non-virtual base classes.


    Destructors, BTW, are always called in the reverse order of constructors. You can rely on that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 409k
  • Answers 409k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer PHP uses hundreds of constants itself, so no problem, you… May 15, 2026 at 7:15 am
  • Editorial Team
    Editorial Team added an answer It is unfortunate that Microsoft have decided to refer to… May 15, 2026 at 7:15 am
  • Editorial Team
    Editorial Team added an answer Remember that masm is a "typed" assembly language. And it… May 15, 2026 at 7:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.