2017-01-20 27 views
1

我的第一篇文章,在此先感谢。 我是C++的新手,我正在努力应对这个错误。C++错误 - <unnamed-tag>一个匿名初始化器不允许匿名工会的成员

typedef struct { 
    int x; 
    int z; 
    char ref[20]; // or of other adequate type 
    DATE date; 
    bool put; 
    int hasPiece = false; 

} TRequest; 

当我建立它在标题

"<unnamed-tag>::hasPiece' : an in-class initializer is not allowed for a member of an anonymous union in non-class scope.

能否请你帮我显示错误?非常感谢

+0

为什么要标记'C#'??! – Null

+2

这不是c#代码,不要将它标记为 – TheLethalCoder

+0

您知道结构名称以及类名称会自动定义为类型名称,对吧?所以你不需要在C++中为结构或类使用'typedef'。也许你需要[一本很好的初学者书籍](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? –

回答

0

在C++ 98中,您不允许将默认值赋予结构体的成员,尽管您可能可以在以后的标准中使用。改为使用此方法:

struct TRequest 
{ 
    TRequest() 
     : hasPiece(0) 
    { 
     // Nothing to do here 
    } 

    int x; 
    int z; 
    char ref[20]; // or of other adequate type 
    DATE date; 
    bool put; 
    int hasPiece; 
}; 

请注意,您应该将所有其他成员初始化,而不仅仅是hasPiece。我还修复了一些其他的部分,你不需要typedef。

编辑:刚才注意到hasPiece是一个int,你为什么将它初始化为false?它应该是一个布尔值,或者初始化为0.我已经更改了我的答案,将其初始化为0.