Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Class "name" has no member class "name of the member"

I`m new to OOP.
Here is the code:

#include <cstdint>
#include <string>
#include <iostream>

namespace name
{
    class Class 
    {
    private:
        struct u
        {
            std::string l{};
            struct u* next{};
        };

    public:
        Class(std::string s)
            :
            u::l(s)
        { }
            
        ~Class() = default;
    };
};

When I compile this I`m getting an errors like this:

Error (active)  E1018   class "name::Class::u" has no member class "l"  
Error   C2614   'name::Class': illegal member initialization: 'l' is not a base or member name

Can you help me. I will glad you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

this

    struct u
    {
        std::string l{};
        struct u* next{};
    };

does not do what you seem to think it does, it is just defining the type ‘u’, it does not say that ‘Class’ contains an instance of ‘u’.

Plus you have to have a constructor in order to use that initialization syntax

namespace name
{
    class Class
    {
    private:
        struct u
        {
            std::string l{};
            struct u* next{};
            u(string s) :l(s) {} <<<=== here is Class::u constructor
        };

        u _u; <<<==== I want Class to contain a Class::u

    public:
        Class(std::string s)
            :
            _u(s) <<< ====use constructor
        {
        }

        ~Class() = default;
    };
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading