小编Ant*_*ony的帖子

如何在类构造函数中初始化匿名结构?

我怎么能初始化匿名结构?

class A
{
public:
    A() : m_var.var_1(1)
    {
    }
    
private:
    struct 
    {
        int var_1;
        int var_2;
    }m_var;
};

Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,上面的代码给了我这个错误:

main.cpp: In constructor ‘A::A()’:
main.cpp:6:16: error: expected ‘(’ before ‘.’ token
    6 |     A() : m_var.var_1(1)
      |                ^
      |                (
main.cpp:6:16: error: expected ‘{’ before ‘.’ token
Run Code Online (Sandbox Code Playgroud)

c++ struct initialization anonymous-types

1
推荐指数
1
解决办法
96
查看次数

标签 统计

anonymous-types ×1

c++ ×1

initialization ×1

struct ×1