declaration does not declare anything : warning?

前端 未结 4 2158
暖寄归人
暖寄归人 2021-02-07 09:25
#include 
#include 
#include 

int main()
{
    struct emp
    {
        struct address
        {
              int a;
             


        
相关标签:
4条回答
  • 2021-02-07 09:39

    The reason why the compiler is showing the warning is because it doesn't see a name for the variable of type address you defined for the emp struct, even though you do declare something using address on the next line, but I guess the compiler is not smart enough to figure that out.

    As you showed, this produces a warning:

    struct emp {
      struct address {}; // This statement doesn't declare any variable for the emp struct.
      struct address a1;
    };
    

    But not this:

    struct emp {
      struct address {} a1; // This statement defines the address struct and the a1 variable.
    };
    

    Or this:

    struct address {};
    
    struct emp {
      struct address a1; //the only statement declare a variable of type struct address
    };
    

    The struct emp {} doesn't show any warnings since this statement is not inside a struct defintion block. If you did put it inside one of those then the compiler will show a warning for that as well. The following will show two warnings:

    struct emp {
      struct phone {};
      struct name {};
    };
    
    0 讨论(0)
  • 2021-02-07 09:41

    Going to also throw this out there that this error may also show up if you don't properly forward declare types found in other namespaces.

    https://stackoverflow.com/a/19001722/1330381

    Example

    namespace A {
    
    // forward declarations
    namespace X {
    namespace Y {
    class MyType;
    }
    }
    
    namespace B {
    
    class Foo() {
        void methodBar(const X::Y::MyType& mt);
    }
    }
    
    0 讨论(0)
  • 2021-02-07 09:42

    The reason the warning is displayed is that the first excerpt is not proper C - it has a constraint violation that a standards-compliant C compiler must produce a diagnostisc message for. It violates the C11 6.7.2.1p2:

    Constraints

    1. A struct-declaration that does not declare an anonymous structure or anonymous union shall contain a struct-declarator-list.

    Meaning that it is OK to write

    struct foo {
        struct {
              int a;
        };
    };
    

    since the inner struct declares an anonymous structure, i.e. it is not named.

    But in your example the struct address has a name - address - and therefore it must have a declarator list after the closing brace - declarator list being for example a1 as in your example, or more complex foo, *bar, **baz[23][45].

    0 讨论(0)
  • 2021-02-07 09:56

    The syntax of a structure definition is:

    struct identifier {
        type member_name;
    
        // ...
    
    };
    

    If you add an identifier just after the closing curly brace, you're declaring a variable with that defined struct.

    In your first example the compiler consider the address struct as member type. it's like if you writes:

    struct identifier {
    
        type ; // No member name is specified
        type a1;
    
        // ...
    
    }
    

    But in the second example you specified the member name:

    struct identifier {
    
        type a1; // Member name specified
    
        // ...
    
    }
    

    And here is an example of the warning: http://ideone.com/KrnYiE.

    0 讨论(0)
提交回复
热议问题