c++ puzzle

Discussion in 'C++' started by Nan Li, Oct 4, 2006.

  1. Nan Li

    Nan Li Guest

    I have questions about the following code. What does the error line try
    to declare ? If the next line can succeed, why does the first one fail
    ? Thanks a lot.

    class A {
    public:
    A( char const * ) {}
    };

    int main()
    {
    char const * a = "hello";
    A(a); //error
    A( (char const *)a ); //ok
    return 0;
    }
     
    Nan Li, Oct 4, 2006
    #1
    1. Advertising

  2. Nan Li

    MC felon Guest

    Nan Li wrote:

    > I have questions about the following code. What does the error line try
    > to declare ? If the next line can succeed, why does the first one fail
    > ? Thanks a lot.
    >
    > class A {
    > public:
    > A( char const * ) {}
    > };
    >
    > int main()
    > {
    > char const * a = "hello";
    > A(a); //error
    > A( (char const *)a ); //ok
    > return 0;
    > }


    here,
    >>A( char const * )

    i tried giving it a name, like 'a'

    A(char const* a) {}

    and then in main, i just :

    char const* p;
    A(p);
    it works perfectly
     
    MC felon, Oct 4, 2006
    #2
    1. Advertising

  3. Nan Li

    Nan Li Guest

    MC felon wrote:
    > Nan Li wrote:
    >
    > > I have questions about the following code. What does the error line try
    > > to declare ? If the next line can succeed, why does the first one fail
    > > ? Thanks a lot.
    > >
    > > class A {
    > > public:
    > > A( char const * ) {}
    > > };
    > >
    > > int main()
    > > {
    > > char const * a = "hello";
    > > A(a); //error
    > > A( (char const *)a ); //ok
    > > return 0;
    > > }

    >
    > here,
    > >>A( char const * )

    > i tried giving it a name, like 'a'
    >
    > A(char const* a) {}
    >
    > and then in main, i just :
    >
    > char const* p;
    > A(p);
    > it works perfectly


    What compiler did you use? I use g++. With your change, I still got the
    same old results. I don't think adding a formal parameter in the
    signature will change things.

    It seems the error line A(a) here is actually the same as 'A a'. I
    also tried 'A (a)', 'A(((a)))', ... they are all the same thing.
     
    Nan Li, Oct 4, 2006
    #3
  4. Nan Li

    MC felon Guest

    i dont know what you're talking about. the code is something that's
    universally accepted in all compilers of c++.it's a plain code of OOP
    that uses no libraries. no matter what compiler you use, it'll be the
    same. enter exactly this code:


    class a
    {
    public:
    a(char const* a) {}
    };

    void main()
    {
    char const* pic;
    a(pic);
    }



    if it doesn't work, i'll dye my hair pink and send you a pic of it.
     
    MC felon, Oct 4, 2006
    #4
  5. "MC felon" <> wrote in message
    news:...
    >i dont know what you're talking about. the code is something that's
    > universally accepted in all compilers of c++.it's a plain code of OOP
    > that uses no libraries. no matter what compiler you use, it'll be the
    > same. enter exactly this code:
    >
    >
    > class a
    > {
    > public:
    > a(char const* a) {}
    > };
    >
    > void main()
    > {
    > char const* pic;
    > a(pic);
    > }
    >
    >
    >
    > if it doesn't work, i'll dye my hair pink and send you a pic of it.


    It seems to be code that's universally rejected on ever compiler I've just
    tried it on. Here's what the Comeau compiler said:

    "ComeauTest.c", line 9: error: "a" has already been declared in the current
    scope
    A(a); //error
    ^

    "ComeauTest.c", line 9: error: no default constructor exists for class "A"
    A(a); //error
    ^

    "ComeauTest.c", line 10: error: no suitable conversion function from "A" to
    "const char *" exists
    A( (char const *)a ); //ok
    ^

    "ComeauTest.c", line 8: warning: variable "a" was declared but never
    referenced
    char const * a = "hello";
    ^

    3 errors detected in the compilation of "ComeauTest.c".

    I think you may be going round with pink hair for a while :)

    Stu

    P.S. It's "int main()"! :) No, seriously...
     
    Stuart Golodetz, Oct 4, 2006
    #5
  6. Nan Li

    MC felon Guest

    but it works in TC++...
     
    MC felon, Oct 4, 2006
    #6
  7. Nan Li

    MC felon Guest

    and i thought

    int main()
    {
    \\something


    return 0;
    }

    and

    void main()
    {
    \\something
    }

    mean the same here
     
    MC felon, Oct 4, 2006
    #7
  8. Nan Li

    lwz Guest

    class a
    {
    public:
    a(const char *A){;}
    };
    int main()
    {
    const char *pic;
    a x(pic);
    return 0;
    }

    The above code *should* work, (I tried it in Dev-Cpp, which uses g++),
    and anyway you cannot call a function that is of a class just like that
    without defining any object (of course of that class) at all... classes
    are like bases for objects... (objects having the same declaration as a
    class, just that you can have many such objects at once like a
    x("a"),y("s"),z("d");
    but you can call x=new a(pic); if i'm not mistaken... correct me if i'm
    wrong.

    Stuart Golodetz wrote:
    > "MC felon" <> wrote in message
    > news:...
    > >i dont know what you're talking about. the code is something that's
    > > universally accepted in all compilers of c++.it's a plain code of OOP
    > > that uses no libraries. no matter what compiler you use, it'll be the
    > > same. enter exactly this code:
    > >
    > >
    > > class a
    > > {
    > > public:
    > > a(char const* a) {}
    > > };
    > >
    > > void main()
    > > {
    > > char const* pic;
    > > a(pic);
    > > }
    > >
    > >
    > >
    > > if it doesn't work, i'll dye my hair pink and send you a pic of it.

    >
    > It seems to be code that's universally rejected on ever compiler I've just
    > tried it on. Here's what the Comeau compiler said:
    >
    > "ComeauTest.c", line 9: error: "a" has already been declared in the current
    > scope
    > A(a); //error
    > ^
    >
    > "ComeauTest.c", line 9: error: no default constructor exists for class "A"
    > A(a); //error
    > ^
    >
    > "ComeauTest.c", line 10: error: no suitable conversion function from "A" to
    > "const char *" exists
    > A( (char const *)a ); //ok
    > ^
    >
    > "ComeauTest.c", line 8: warning: variable "a" was declared but never
    > referenced
    > char const * a = "hello";
    > ^
    >
    > 3 errors detected in the compilation of "ComeauTest.c".
    >
    > I think you may be going round with pink hair for a while :)
    >
    > Stu
    >
    > P.S. It's "int main()"! :) No, seriously...
     
    lwz, Oct 4, 2006
    #8
  9. Nan Li

    lwz Guest

    And yes, in Turbo-C++ you can use void main... but not in other
    compilers as they require a return value... if you would notice, in
    Turbo-C++ they also give you a warning when you do that... just that in
    other compilers it gives an error.

    MC felon wrote:
    > and i thought
    >
    > int main()
    > {
    > \\something
    >
    >
    > return 0;
    > }
    >
    > and
    >
    > void main()
    > {
    > \\something
    > }
    >
    > mean the same here
     
    lwz, Oct 4, 2006
    #9
  10. Nan Li

    MC felon Guest

    oh... yeahh, i meant to include naming the object..
    so instead of

    a(pic);

    it's


    a object_of_a(pic);
    sorry!
    anyway, it'll work now or i'll die my hair red.
     
    MC felon, Oct 4, 2006
    #10
  11. Nan Li

    lwz Guest

    I just checked the last statement out (the new statement)...

    a *x;
    x=new a(pic);

    The above should work also. Just that call members of the class using
    the -> operator, since x is a pointer to the object.

    lwz wrote:
    > class a
    > {
    > public:
    > a(const char *A){;}
    > };
    > int main()
    > {
    > const char *pic;
    > a x(pic);
    > return 0;
    > }
    >
    > The above code *should* work, (I tried it in Dev-Cpp, which uses g++),
    > and anyway you cannot call a function that is of a class just like that
    > without defining any object (of course of that class) at all... classes
    > are like bases for objects... (objects having the same declaration as a
    > class, just that you can have many such objects at once like a
    > x("a"),y("s"),z("d");
    > but you can call x=new a(pic); if i'm not mistaken... correct me if i'm
    > wrong.
    >
    > Stuart Golodetz wrote:
    > > "MC felon" <> wrote in message
    > > news:...
    > > >i dont know what you're talking about. the code is something that's
    > > > universally accepted in all compilers of c++.it's a plain code of OOP
    > > > that uses no libraries. no matter what compiler you use, it'll be the
    > > > same. enter exactly this code:
    > > >
    > > >
    > > > class a
    > > > {
    > > > public:
    > > > a(char const* a) {}
    > > > };
    > > >
    > > > void main()
    > > > {
    > > > char const* pic;
    > > > a(pic);
    > > > }
    > > >
    > > >
    > > >
    > > > if it doesn't work, i'll dye my hair pink and send you a pic of it.

    > >
    > > It seems to be code that's universally rejected on ever compiler I've just
    > > tried it on. Here's what the Comeau compiler said:
    > >
    > > "ComeauTest.c", line 9: error: "a" has already been declared in the current
    > > scope
    > > A(a); //error
    > > ^
    > >
    > > "ComeauTest.c", line 9: error: no default constructor exists for class "A"
    > > A(a); //error
    > > ^
    > >
    > > "ComeauTest.c", line 10: error: no suitable conversion function from "A" to
    > > "const char *" exists
    > > A( (char const *)a ); //ok
    > > ^
    > >
    > > "ComeauTest.c", line 8: warning: variable "a" was declared but never
    > > referenced
    > > char const * a = "hello";
    > > ^
    > >
    > > 3 errors detected in the compilation of "ComeauTest.c".
    > >
    > > I think you may be going round with pink hair for a while :)
    > >
    > > Stu
    > >
    > > P.S. It's "int main()"! :) No, seriously...
     
    lwz, Oct 4, 2006
    #11
  12. Nan Li

    Zara Guest

    On 3 Oct 2006 23:15:17 -0700, "Nan Li" <> wrote:

    >I have questions about the following code. What does the error line try
    >to declare ? If the next line can succeed, why does the first one fail
    >? Thanks a lot.
    >
    >class A {
    > public:
    > A( char const * ) {}
    >};
    >
    >int main()
    >{
    > char const * a = "hello";
    > A(a); //error
    > A( (char const *)a ); //ok
    > return 0;
    >}


    It may be a surprise to you, but your error line is equivalent to:

    A a;

    So you are defining a variable a of type A (which has already been
    defined, and with other type), and initialising it via the default
    contructor,(which is undefined). Two errors (at least!) in one line.

    Instead, the other line is telling the compiuler to create a temporary
    of type A, initializing it with the constructor declared for const
    char *, and then discard it. There is no error, but it is useless as
    such class and such constructor have no side effects.

    Best regards,

    Zara
     
    Zara, Oct 4, 2006
    #12
  13. Nan Li

    Howard Guest

    "MC felon" <> wrote in message
    news:...
    > but it works in TC++...
    >


    This is not a chat room! How about quoting what you're responding to, eh?

    -Howard
     
    Howard, Oct 4, 2006
    #13
  14. Nan Li

    Earl Purple Guest

    Nan Li wrote:

    > I have questions about the following code. What does the error line try
    > to declare ? If the next line can succeed, why does the first one fail
    > ? Thanks a lot.
    >
    > class A {
    > public:
    > A( char const * ) {}
    > };
    >
    > int main()
    > {
    > char const * a = "hello";
    > A(a); //error


    You are trying to create a temporary of type A but the compiler doesn't
    think you are. Bind it to a const reference and the line will work so

    const A& temp = A(a);

    > A( (char const *)a ); //ok
    > return 0;
    > }


    If you don't want the A to live even as long as temp for whatever
    reason you can create a dummy function:

    void do_nothing( const A& a );

    do_nothing( A( a ) );
     
    Earl Purple, Oct 4, 2006
    #14
  15. Nan Li

    red floyd Guest

    MC felon wrote:
    > but it works in TC++...
    >


    so TC++ is "all compilers of c++"?

    The OP's problem is that it's actually declaring a second variable named
    "a" of type A. The problem is that he's already got a variable named "a".

    The compiler will do a lot of stripping of parens on declarations.
    Google for "most vexing parse", to see the canonical/extreme example.
     
    red floyd, Oct 4, 2006
    #15
  16. Nan Li posted:

    > char const * a = "hello";
    > A(a); //error



    As someone else has already pointed out, the error line is interpretted as:

    A a;

    , which of course is a definition of an object. Two remedies:

    1.
    (A)a;

    2.
    (void)A(a);

    If you fathom in any way that it could possibly be a declaration, then
    it's a declaration.

    class MyClass {
    public:

    MyClass() {}

    MyClass(int) {}

    MyClass(char,int*,double) {}
    };

    int main()
    {
    int a;

    MyClass(); /* No problem! */

    MyClass(4); /* No problem! */

    MyClass(a+2); /* No problem! */

    MyClass(a); /* ERROR: This is a declaration! */

    (MyClass)a; /* No problem! */

    (void)MyClass(a);
    }

    --

    Frederick Gotham
     
    Frederick Gotham, Oct 4, 2006
    #16
  17. Nan Li

    Gavin Deane Guest

    MC felon wrote:
    > and i thought
    >
    > int main()
    > {
    > \\something
    >
    >
    > return 0;
    > }
    >
    > and
    >
    > void main()
    > {
    > \\something
    > }
    >
    > mean the same here


    No.

    int main()
    {
    // something
    return 0;
    }

    and

    int main()
    {
    // something
    }

    mean the same thing. If you leave out the return statement at the end
    of main, the compiler inserts an implicit return 0;

    void main()
    {
    // anything
    }

    is not correct C++. Any compiler that accepts it (and many do) is doing
    so as a non-standard extension to the language.

    Gavin Deane
     
    Gavin Deane, Oct 4, 2006
    #17
    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. Earl Teigrob
    Replies:
    3
    Views:
    6,652
    Nedu N
    Aug 6, 2003
  2. dwa

    Design Puzzle!

    dwa, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    369
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jun 10, 2004
  3. Shankara Narayanan

    Booking puzzle....

    Shankara Narayanan, Jun 17, 2004, in forum: ASP .Net
    Replies:
    20
    Views:
    931
    bredal Jensen
    Jun 30, 2004
  4. VB Programmer
    Replies:
    2
    Views:
    430
    Alan Lambert
    Nov 4, 2004
  5. G. Stewart

    regex puzzle!

    G. Stewart, Nov 23, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    518
    G. Stewart
    Nov 25, 2004
Loading...

Share This Page