Why must the struct (;) be deleted to avoid error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    Why must the struct (;) be deleted to avoid error

    //if the ; is not deleted L5 the code will not compile.
    //the error is shown below.
    //without the ; the code runs as intended.
    //could someone explain why?
    Code:
    #include<iostream>
    
    struct Date {
    int month,day,year;
    }//;
    birthdays[ ]={
        {12,17,37},
        {10,31,38},
        {6,24,40},
        {11,23,42},
        {8,5,44},
    };
    const Date& getdate(int n)
    {
        return birthdays[n-1];
    }
    
    int main()
    {
        int dt=99;
        while(dt!=0) {
        std::cout<<std::endl
                 <<"Enter datr #(1-5, 0 to quit):";
                 std::cin>>dt;
                 if(dt>0 && dt<6){
                 const Date& bd=getdate(dt);
                 std::cout<<bd.month<<'/'
                          <<bd.day<<'/'
                          <<bd.year<<std::endl;
                 }
        }
     return 0;
    }
    /*
    --->> error: expected constructor, destructor, or type conversion before '=' token|
    
    Enter datr #(1-5, 0 to quit):3
    6/24/40
    
    Enter datr #(1-5, 0 to quit):1
    12/17/37
    
    Enter datr #(1-5, 0 to quit):5
    8/5/44
    
    Enter datr #(1-5, 0 to quit):0
    
    Process returned 0 (0x0)   execution time : 35.750 s
    Press any key to continue.*/
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Look carefully at your code.

    A struct can be declared in order to create a variable:

    Code:
    struct X {
    
    int member;
    }aVar;
    Without that semi-colon you have:

    Code:
    struct Date {
    int month, day, year;
    }birthday [] etc...
    This is a variable named birthdays of type Date.

    Code:
    struct Date {
    int month, day, year;
    };birthday [] etc...
    With semi-colon in there, the struct declaration is complete.
    Now the birthday array has no type and C++ will not assume int. So your compile fails.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      To weaknessforcats ,
      thanks for your explanation. I feel this question has been fully answered.

      Comment

      Working...