Class "name" 没有成员 class "name of the member"

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

我是 OOP 新手。 这是代码:

#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;
    };
};

当我编译这个时,我收到这样的错误:

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

你能帮帮我吗?我会很高兴你。

这个

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

并没有像你认为的那样做,它只是定义了类型 'u',它并没有说 'Class' 包含 'u' 的实例。

此外,您必须有一个构造函数才能使用该初始化语法

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