Silly question (8 lines of code)

Discussion in 'C++' started by Manuel, Dec 28, 2005.

  1. Manuel

    Manuel Guest

    Why this not work?
    -----------------------------
    class CRectangle {
    public:
    void set_values ();
    };

    void CRectangle::set_values () {}

    CRectangle rect;
    rect.set_values ();

    int main () {}
    ------------------------------


    And this work?

    -----------------------------
    class CRectangle {
    public:
    void set_values ();
    };

    void CRectangle::set_values () {}

    CRectangle rect;


    int main () {rect.set_values ();}
    ------------------------------

    Thanks!
     
    Manuel, Dec 28, 2005
    #1
    1. Advertising

  2. Manuel

    W Marsh Guest

    Manuel wrote:
    > Why this not work?
    > -----------------------------
    > class CRectangle {
    > public:
    > void set_values ();
    > };
    >
    > void CRectangle::set_values () {}
    >
    > CRectangle rect;
    > rect.set_values ();
    >
    > int main () {}
    > ------------------------------
    >
    >
    > And this work?
    >
    > -----------------------------
    > class CRectangle {
    > public:
    > void set_values ();
    > };
    >
    > void CRectangle::set_values () {}
    >
    > CRectangle rect;
    >
    >
    > int main () {rect.set_values ();}
    > ------------------------------
    >
    > Thanks!


    Because you are doing work outside of program flow. Areas outside of
    functions/methods are for things such as declarations, not actual work.
     
    W Marsh, Dec 28, 2005
    #2
    1. Advertising

  3. Manuel wrote:
    > Why this not work?
    > -----------------------------
    > class CRectangle {
    > public:
    > void set_values ();
    > };
    >
    > void CRectangle::set_values () {}
    >
    > CRectangle rect;
    > rect.set_values ();
    >
    > int main () {}
    > ------------------------------
    >
    >
    > And this work?
    >
    > -----------------------------
    > class CRectangle {
    > public:
    > void set_values ();
    > };
    >
    > void CRectangle::set_values () {}
    >
    > CRectangle rect;
    >
    >
    > int main () {rect.set_values ();}
    > ------------------------------
    >
    > Thanks!



    Hello,

    In your first non-working example, what is going wrong (please provide
    the complete error message) and what do you expect from the program? My
    guess is that the compiler produces code that does nothing since you do
    nothing in your main?

    Best regards,
    Peter
     
    Peter Jansson, Dec 28, 2005
    #3
  4. Manuel

    mlimber Guest

    Manuel wrote:
    > Why this not work?
    > -----------------------------
    > class CRectangle {
    > public:
    > void set_values ();
    > };
    >
    > void CRectangle::set_values () {}
    >
    > CRectangle rect;
    > rect.set_values ();
    >
    > int main () {}
    > ------------------------------
    >
    >
    > And this work?
    >
    > -----------------------------
    > class CRectangle {
    > public:
    > void set_values ();
    > };
    >
    > void CRectangle::set_values () {}
    >
    > CRectangle rect;
    >
    >
    > int main () {rect.set_values ();}
    > ------------------------------
    >
    > Thanks!


    A program is a *sequence* of instructions, which in C++ means that when
    all initialization is complete, main() is invoked. All program behavior
    must result from one of those two steps. In your first example, the
    call to CRectangle::set_values() doesn't fit in that scheme since it is
    neither part of initialization nor a consequence of calling main(). In
    the second example, it is the latter. You could make the call part of
    the pre-main initialization like this:

    struct CRectangle
    {
    bool set_values() { return true; }
    };

    namespace // anonymous namespace for file-scope variables
    {
    CRectangle rect;
    const bool init = rect.set_values();
    }

    int main() {}

    Cheers! --M
     
    mlimber, Dec 28, 2005
    #4
  5. Manuel

    Mike Wahler Guest

    "Manuel" <> wrote in message
    news:43b2a2a1$0$1073$...
    > Why this not work?
    > -----------------------------
    > class CRectangle {
    > public:
    > void set_values ();
    > };
    >
    > void CRectangle::set_values () {}
    >
    > CRectangle rect;
    > rect.set_values ();


    Executable statements must be inside a function.

    This one is nt.

    >
    > int main () {}
    > ------------------------------
    >
    >
    > And this work?
    >
    > -----------------------------
    > class CRectangle {
    > public:
    > void set_values ();
    > };
    >
    > void CRectangle::set_values () {}
    >
    > CRectangle rect;
    >
    >
    > int main () {


    rect.set_values ();

    This executable statement is inside a function, so it's OK.

    }
    > ------------------------------
    >
    > Thanks!


    -Mike
     
    Mike Wahler, Dec 28, 2005
    #5
  6. Manuel

    Manuel Guest

    Peter Jansson wrote:

    > In your first non-working example, what is going wrong (please provide
    > the complete error message) and what do you expect from the program? My
    > guess is that the compiler produces code that does nothing since you do
    > nothing in your main?


    No...the error is strange:

    error: expected init-declarator before '.' token
    error: expected `,' or `;' before '.' token
     
    Manuel, Dec 28, 2005
    #6
  7. Manuel

    Manuel Guest

    Mike Wahler wrote:

    >>CRectangle rect;
    >>rect.set_values ();

    >
    >
    > Executable statements must be inside a function.


    I was thinking set_values() itself was already a function...
     
    Manuel, Dec 28, 2005
    #7
  8. Manuel

    Manuel Guest

    mlimber wrote:

    > A program is a *sequence* of instructions, which in C++ means that when
    > all initialization is complete, main() is invoked. All program behavior
    > must result from one of those two steps. In your first example, the
    > call to CRectangle::set_values() doesn't fit in that scheme since it is
    > neither part of initialization nor a consequence of calling main(). In
    > the second example, it is the latter. You could make the call part of
    > the pre-main initialization like this:


    OK...I think to have understand: the keyword is *sequence*: the function
    was outside the main and was "alone", not called in calls tree started
    from main(). It's correct?

    However the error was very strange (make me crazy):
    --------------
    error: expected init-declarator before '.' token
    error: expected `,' or `;' before '.' token
    --------------

    THANKS!!
     
    Manuel, Dec 28, 2005
    #8
  9. Manuel

    Howard Guest

    "Manuel" <> wrote in message
    news:43b2aa4e$0$1063$...
    > Mike Wahler wrote:
    >
    >>>CRectangle rect;
    >>>rect.set_values ();

    >>
    >>
    >> Executable statements must be inside a function.

    >
    > I was thinking set_values() itself was already a function...


    It is a _call_ to a function. Perhaps what he should have said was
    "Executable statements must be inside a function _definition_."
    Declarations of variables (including initialization of those variables) may
    reside at global scope, but not executable statements which are _not_
    initialization sequences.

    This is legal:

    int MyGlobal = 5;

    This is also legal:

    int MyGlobal ;
    void f()
    {
    MyGlobal = 5;
    }

    But this is not:

    int MyGlobal ;
    MyGlobal = 5;

    The error message was caused by an invalid sequence of tokens. The parser
    saw the token "rect" in the line "rect.set_calues ();", and recognized that
    as an identifier (i.e., a name for something). So far, that's not
    _neccessarily_ an error. But the "." following it is, because there is no
    legal statement that the parser could identify which begins with "rect.",
    (at least not when outside of any function definition).

    That's the way with error messages...sometimes you have to look at the code
    _before_ what it flags as an error, or at the whole statement containing the
    error, not just at the one symbol or identifier that it flags as an error.
    In this case, it's the whole statement that's illegal (when outside of any
    function definition).

    -Howard
     
    Howard, Dec 28, 2005
    #9
  10. Manuel

    Manuel Guest

    Howard wrote:

    > The error message was caused by an invalid sequence of tokens. The parser
    > saw the token "rect" in the line "rect.set_calues ();", and recognized that
    > as an identifier (i.e., a name for something). So far, that's not
    > _neccessarily_ an error. But the "." following it is, because there is no
    > legal statement that the parser could identify which begins with "rect.",
    > (at least not when outside of any function definition).
    >
    > That's the way with error messages...sometimes you have to look at the code
    > _before_ what it flags as an error, or at the whole statement containing the
    > error, not just at the one symbol or identifier that it flags as an error.
    > In this case, it's the whole statement that's illegal (when outside of any
    > function definition).


    Thanks.
    It's all clear now !
    :)
     
    Manuel, Dec 28, 2005
    #10
  11. Manuel

    Jay Nabonne Guest

    On Wed, 28 Dec 2005 15:31:59 +0000, Howard wrote:
    > It is a _call_ to a function. Perhaps what he should have said was
    > "Executable statements must be inside a function _definition_."
    > Declarations of variables (including initialization of those variables) may
    > reside at global scope, but not executable statements which are _not_
    > initialization sequences.
    >
    > This is legal:
    >
    > int MyGlobal = 5;
    >
    > This is also legal:
    >
    > int MyGlobal ;
    > void f()
    > {
    > MyGlobal = 5;
    > }
    >
    > But this is not:
    >
    > int MyGlobal ;
    > MyGlobal = 5;
    >


    However, this is also legal:

    int f()
    {
    return 5;
    }

    int MyGlobal = f();

    even outside main.

    So function calls for purposes of initialization are valid.

    - Jay
     
    Jay Nabonne, Dec 28, 2005
    #11
  12. I am afraid. How are your programs working with out returning any
    values from main()

    int main()
    {
    return 0; // finally my compiler asks me to return an integer
    // if I specified return type as int
    // and if return type is void you may not return any
    thing.
    }

    Plz let know what compiler you are using.
     
    Murali Krishna, Dec 29, 2005
    #12
  13. Murali Krishna wrote:
    > I am afraid. How are your programs working with out returning any
    > values from main()
    >
    > int main()
    > {
    > return 0; // finally my compiler asks me to return an integer
    > // if I specified return type as int
    > // and if return type is void you may not return any
    > thing.
    > }


    Two things:

    1) not returning anything from main() is like returning 0

    int main()
    {
    }

    and

    int main()
    {
    return 0;
    }

    are both valid and they do the exact same thing.

    2) void main() is illegal. The only return type main() can have is int.


    Jonathan
     
    Jonathan Mcdougall, Dec 29, 2005
    #13
  14. Manuel

    Manuel Guest

    Murali Krishna wrote:
    > I am afraid. How are your programs working with out returning any
    > values from main()
    >
    > int main()
    > {
    > return 0; // finally my compiler asks me to return an integer
    > // if I specified return type as int
    > // and if return type is void you may not return any
    > thing.
    > }
    >
    > Plz let know what compiler you are using.
    >




    Dev c++

    thx,

    Manuel
     
    Manuel, Dec 29, 2005
    #14
  15. Manuel

    BobR Guest

    Manuel wrote in message <43b3985b$0$1069$>...
    >Murali Krishna wrote:
    >> I am afraid. How are your programs working with out returning any
    >> values from main()
    >>
    >> int main(){
    >> return 0; // finally my compiler asks me to return an integer
    >> // if I specified return type as int
    >> // and if return type is void you may not return any thing.
    >> }
    >>
    >> Plz let know what compiler you are using.

    >
    >
    >Dev c++
    >thx,
    > Manuel


    FYI: That is an IDE. The default compiler for DevC++ is GCC, MinGW port.
    You can set-up DevC++ to use other compilers (..but, it's not fun!! <G>).

    --
    Bob R
    POVrookie
    --
    Dev-C++ IDE: http://www.bloodshed.net/
    MinGW (GNU compiler): http://www.mingw.org/
    MinGWStudio http://www.parinyasoft.com/
    wxWidgets URL: http://www.wxwidgets.org
    V IDE & V GUI: http://www.objectcentral.com/
    Quincy IDE 2005 URL: http://pipou.net/down/Quincy2005Project.zip
    POVray: http://www.povray.org/
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
     
    BobR, Dec 29, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don

    Silly question....

    Don, Sep 26, 2003, in forum: VHDL
    Replies:
    2
    Views:
    494
  2. WuyaSea Operator
    Replies:
    48
    Views:
    1,517
    Ben Phillips
    Sep 24, 2007
  3. Lew
    Replies:
    0
    Views:
    428
  4. Alf P. Steinbach

    Some silly code for Easter holiday

    Alf P. Steinbach, Mar 23, 2010, in forum: Python
    Replies:
    0
    Views:
    317
    Alf P. Steinbach
    Mar 23, 2010
  5. Robert Klemme

    Silly Code from The Muppet Laboratories

    Robert Klemme, Feb 2, 2007, in forum: Ruby
    Replies:
    3
    Views:
    91
    Phrogz
    Feb 2, 2007
Loading...

Share This Page