为什么struct对象不是一个类型?

Mod*_*art 0 c++ struct

我确信我误解了一些事情.

在类中实例化一个struct对象并将其作为构造函数中的值传递后,我得到一个错误?

错误:'test'不是一种类型

#include <iostream>
using namespace std;

struct Test
{
    int x = 11;
    int y = 22; 
};

class A
{
private:
    int foo = 100;      
public:
    A(Test tmp){}
};
class B
{
private:
    Test test;
    A a(test);   //error    
public:
    B(){}   
};

int main()
{
    B b;    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

son*_*yao 5

如果要声明a为数据成员并对其进行初始化,则正确的语法应为:

class B
{
private:
    Test test;
    A a{test};       // default member initializer    
    A a = test;      // default member initializer    
    A a = A(test);   // default member initializer    
public:
    B() : a(test) {} // member initializer list   
};
Run Code Online (Sandbox Code Playgroud)

请注意,默认成员初始值设定项仅支持大括号或等号,但不支持括号初始值设定项.仅当成员初始化列表中的成员被省略时才使用它.

编译器试图将其解释a为一个函数,它返回Atest作为参数.test不是类型名称,然后编译失败.