【问题标题】:How to fix (29,20): error CS1519: Invalid token '}' in class, struct, or interface member declaration如何修复 (29,20):错误 CS1519:类、结构或接口成员声明中的标记“}”无效
【发布时间】:2019-04-15 10:38:35
【问题描述】:

无论我做什么,我都无法编译它

我已经尝试了所有可能的更改 帮我编译一下,对我来说代码很完美,但还是不行

 [System.Serializable]
public class UnderWaterParameters {
    [Header("The following parameters apply for underwater only!")]
    [Space(5)]
    public float fogDensity = 0.1f;
    public Color fogColor;
#if UNITY_POST_PROCESSING_STACK_V1 && AQUAS_PRESENT
    [Space(5)]
    [Header("Post Processing Profiles (Must NOT be empty!)")]
    [Space(5)]
    public PostProcessingProfile underwaterProfile;
    public PostProcessingProfile defaultProfile;
#endif

#if UNITY_POST_PROCESSING_STACK_V2 && AQUAS_PRESENT
    [Space(5)]
    [Header("Post Processing Profiles (Must NOT be empty!)")]
    [Space(5)]
    public PostProcessingProfile underwaterProfile;
    public PostProcessingProfile defaultProfile;
#endif
}

【问题讨论】:

  • 我确定该错误会告诉您它的位置。乍一看,我会说:你在类的末尾有三个不必要的属性(其中一个甚至两次)。必须将属性应用于成员,但只会出现意想不到的}
  • 嗯.......结尾}在#if中,所以如果那不是真的,则没有右大括号,但似乎没有另一个,因为有2个开头的。
  • 如果你能发布足够多的代码让我们可以看到结尾的花括号,那就太好了。此外,#if 中有一个结束大括号,这意味着它可能包含在实际代码中,也可能不包含在实际代码中。如果您在这样的预编译器语句中使用花括号,最好确保开始和结束花括号都在具有相同表达式的 #if 语句中,以便将它们一起包含或排除。
  • 属性(例如[Space(5)])必须始终位于成员、类或任何其他符号之前。那么最后三行应该适用于哪一个呢?
  • @Tayyab 也许您想要#elseif 而不是第二个#if?无论哪种方式,如果只有属性发生变化,那么只将属性放在条件编译块中可能更有意义。

标签: c# unity3d


【解决方案1】:

Attribute(这是您使用 [...] 定义的)提供关于类或类成员的元数据。换句话说,它不能单独存在,它总是要先于某个符号。这就是为什么它不能编译:

#if UNITY_POST_PROCESSING_STACK_V2 && AQUAS_PRESENT
    [Space(5)]
    [Header("Post Processing Profiles (Must NOT be empty!)")]
    [Space(5)] } <-- attribute must preceed a member or class
#endif

在我看来,你想要的只是基于两个条件中的哪一个适用的不同属性,所以你应该只将那些行包装到 #if #elif 中,这应该是不同的,而不是那些常见的 .

[System.Serializable]
public class UnderWaterParameters {
    [Header("The following parameters apply for underwater only!")]
    [Space(5)]
    public float fogDensity = 0.1f;
    public Color fogColor;
#if UNITY_POST_PROCESSING_STACK_V1 && AQUAS_PRESENT
    [Space(5)]
    [Header("Post Processing Profiles (Must NOT be empty!)")]
    [Space(5)]
#elif UNITY_POST_PROCESSING_STACK_V2 && AQUAS_PRESENT
    [Space(5)]
    [Header("Post Processing Profiles (Must NOT be empty!)")]
    [Space(5)] }
#endif
    public PostProcessingProfile underwaterProfile;
    public PostProcessingProfile defaultProfile;
}

当然,上述内容没有多大意义,因为在两种情况下都应用了完全相同的属性。但这对我来说似乎是另一个问题。

另请参阅 MSDN 中的文档:https://docs.microsoft.com/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-elif

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多