Home > OS >  Class "name" has no member class "name of the member"
Class "name" has no member class "name of the member"

Time:04-29

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.

CodePudding user response:

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;
    };
};
  • Related