is this a C++ BUG?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nrip
    New Member
    • Nov 2006
    • 16

    is this a C++ BUG?

    Dear all,
    I was just having fun by writing a simple class in C++ today and i found out this
    [code=cpp]
    #include<iostre am.h>

    class A
    {
    public :
    int a;
    }

    main()
    {
    A b;
    b.a = 5;
    cout<<"The value of a is:"<<b.a;
    }[/code]

    this code works absolutely fine even though the class is not closed with a semicolon ";" and if i replace in the above code the main function by VOID MAIN() then it doesn't work!!!!

    Is there any defined reason for this thing happening?????

    Thanks in advance.

    Regards
    Nrip
    Last edited by sicarie; Sep 17 '07, 01:00 PM. Reason: Code tags
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I would guess that it's your compiler. It's a good thing you're not getting a program working with void main - it should be int main, I'm surprised that just main works.

    As for the class, I'm guessing it works either way on your compiler (closed or not), and when you don't have an ending semi-colon, the compiler is closing the class itself when the main instantiates. (These are two separate issues, BTW)

    Comment

    • kreagan
      New Member
      • Aug 2007
      • 153

      #3
      Originally posted by sicarie
      It's a good thing you're not getting a program working with void main - it should be int main
      *Blink Blink* What's wrong with void main?

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by kreagan
        *Blink Blink* What's wrong with void main?
        I think others can explain it better than I.

        The short: ANSI says so. The discussion: http://users.aber.ac.uk/auj/voidmain.shtml

        Comment

        • ashitpro
          Recognized Expert Contributor
          • Aug 2007
          • 542

          #5
          this is not a bug.
          When you skip the semicolon at the end of class,
          compiler interpret that main is returning class A.
          But when you specify explicit void,it gives error

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by kreagan
            *Blink Blink* What's wrong with void main?
            About everything; the exit() function implementation calls main(...) and expects
            a return value; an int. I main() happens to be void or whatever, popping the return
            value in a stack based machine may corrupt the execution stack.

            No matter what Microsoft wants you to believe; they're wrong; the return type
            of main() is int, int and just int; not void or whatever. Both the C and C++ Standards
            state so in a 'shall' clause which means that it *must* be done so in order to be
            compliant with the standard. Everyone who does otherwise hasn't understood
            what these Standards are about, e.g. Microsoft.

            kind regards,

            Jos

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by JosAH
              No matter what Microsoft wants you to believe; they're wrong;
              If you disable language extensions on your project, this will draw a warning from Visual Studio.NET 2005. Kinda like using g++ with the -pedantic switch.

              Comment

              Working...