Problem with C++ traits under OSX

Discussion in 'C++' started by Sargon, Jul 22, 2004.

  1. Sargon

    Sargon Guest

    Hi

    I have a problem with C++ traits. The following lil program provides a
    Wrapper class which contains 3 static methods. (1 normal, 2 as a
    template)
    ======================================================
    #include <iostream>
    using namespace std;

    enum colour_t { White, Black };


    static void f(void);
    template <colour_t c> static void f(void);


    static void f(void)
    {
    cout << "Hello from colourless f" << endl;
    }


    template <colour_t c>
    void f(void)
    {
    if(c == White) cout << "Hello from white f" << endl;
    else cout << "Hello from black f" << endl;
    }


    int main(void)
    {
    f();
    f<White>();
    f<Black>();

    return 0;
    }
    ======================================================

    The thing compiles w/o problems under Linux (gcc 3.3.3 under gentoo)
    and Cygwin (gcc 3.3.1 under WinNT) and produces the expected output:

    Hello from colourless f
    Hello from white f
    Hello from black f


    Under MacOSX however (gcc 3.3 20030304) gives the following compile
    error:

    test.cxx:23: error: redefinition of `static void Wrapper::f()'
    test.cxx:16: error: `static void Wrapper::f()' previously declared
    here
    test.cxx:23: error: no `static void Wrapper::f()' member function
    declared in


    If the static functions are defined outside the wrapper class, it
    compiles fine.

    Any idea what's wrong? I'm asking in a non-OSX newsgroup because I'd
    like to know whether the program above is really standard-compliant or
    not.

    Regards,

    Sargon
    Sargon, Jul 22, 2004
    #1
    1. Advertising

  2. Sargon wrote:

    > Hi
    >
    > I have a problem with C++ traits. The following lil program provides a
    > Wrapper class which contains 3 static methods. (1 normal, 2 as a
    > template)
    > ======================================================
    > #include <iostream>
    > using namespace std;
    >
    > enum colour_t { White, Black };
    >
    >
    > static void f(void);
    > template <colour_t c> static void f(void);
    >
    >
    > static void f(void)
    > {
    > cout << "Hello from colourless f" << endl;
    > }
    >
    >
    > template <colour_t c>
    > void f(void)
    > {
    > if(c == White) cout << "Hello from white f" << endl;
    > else cout << "Hello from black f" << endl;
    > }
    >
    >
    > int main(void)
    > {
    > f();
    > f<White>();
    > f<Black>();
    >
    > return 0;
    > }
    > ======================================================
    >
    > The thing compiles w/o problems under Linux (gcc 3.3.3 under gentoo)
    > and Cygwin (gcc 3.3.1 under WinNT) and produces the expected output:
    >
    > Hello from colourless f
    > Hello from white f
    > Hello from black f
    >
    >
    > Under MacOSX however (gcc 3.3 20030304) gives the following compile
    > error:
    >
    > test.cxx:23: error: redefinition of `static void Wrapper::f()'
    > test.cxx:16: error: `static void Wrapper::f()' previously declared
    > here
    > test.cxx:23: error: no `static void Wrapper::f()' member function
    > declared in
    >
    >
    > If the static functions are defined outside the wrapper class, it
    > compiles fine.
    >
    > Any idea what's wrong? I'm asking in a non-OSX newsgroup because I'd
    > like to know whether the program above is really standard-compliant or
    > not.
    >
    > Regards,
    >
    > Sargon

    Hi Sargon,
    I don't know what the C++ 'law' on this is, but I have come across your
    problem in my own C++ code. The 'normal' static method you have must
    only be declared in the header of Wrapper, and not defined. So you
    should define it in Wrapper.cpp. I actually think this is probably how
    it is supposed to be in C++, but couldn't say what the standard says.

    Drew McCormack
    Drew McCormack, Jul 22, 2004
    #2
    1. Advertising

  3. Sargon

    tom_usenet Guest

    On 22 Jul 2004 00:38:34 -0700, (Sargon) wrote:

    >Hi
    >
    >I have a problem with C++ traits. The following lil program provides a
    >Wrapper class which contains 3 static methods. (1 normal, 2 as a
    >template)


    No it doesn't, your code contains no "Wrapper" class at all!

    >Under MacOSX however (gcc 3.3 20030304) gives the following compile
    >error:
    >
    >test.cxx:23: error: redefinition of `static void Wrapper::f()'
    >test.cxx:16: error: `static void Wrapper::f()' previously declared
    >here
    >test.cxx:23: error: no `static void Wrapper::f()' member function
    >declared in
    >
    >
    >If the static functions are defined outside the wrapper class, it
    >compiles fine.
    >
    >Any idea what's wrong? I'm asking in a non-OSX newsgroup because I'd
    >like to know whether the program above is really standard-compliant or
    >not.


    Without seeing the real code, it's hard to say what the problem is.
    Note that you can't separately declare and define a method inside a
    class definition. e.g.

    class C
    {
    void f();
    void f()
    {
    }
    };

    is not legal, but we can't tell whether that's what you're doing,
    since you haven't posted the code.

    Tom
    tom_usenet, Jul 22, 2004
    #3
  4. Sargon

    Sargon Guest

    tom_usenet <> wrote in message news:<>...
    > On 22 Jul 2004 00:38:34 -0700, (Sargon) wrote:
    >
    > >Hi
    > >
    > >I have a problem with C++ traits. The following lil program provides a
    > >Wrapper class which contains 3 static methods. (1 normal, 2 as a
    > >template)

    >
    > No it doesn't, your code contains no "Wrapper" class at all!


    Oh my... I mistakenly posted the modified code... here's the
    correct one:

    ==================================================
    #include <iostream>
    using namespace std;

    enum colour_t { White, Black };


    class Wrapper
    {
    public:
    static void f(void);
    template <colour_t c> static void f(void);
    };


    void Wrapper::f(void)
    {
    cout << "Hello from colourless f" << endl;
    }


    template <colour_t c>
    void Wrapper::f(void)
    {
    if(c == White) cout << "Hello from white f" << endl;
    else cout << "Hello from black f" << endl;
    }


    int main(void)
    {
    Wrapper::f();
    Wrapper::f<White>();
    Wrapper::f<Black>();

    return 0;
    }
    ==================================================


    > >Under MacOSX however (gcc 3.3 20030304) gives the following compile
    > >error:
    > >
    > >test.cxx:23: error: redefinition of `static void Wrapper::f()'
    > >test.cxx:16: error: `static void Wrapper::f()' previously declared
    > >here
    > >test.cxx:23: error: no `static void Wrapper::f()' member function
    > >declared in
    > >
    > >
    > >If the static functions are defined outside the wrapper class, it
    > >compiles fine.
    > >
    > >Any idea what's wrong? I'm asking in a non-OSX newsgroup because I'd
    > >like to know whether the program above is really standard-compliant or
    > >not.
    Sargon, Jul 22, 2004
    #4
  5. Sargon

    tom_usenet Guest

    On 22 Jul 2004 06:19:55 -0700, (Sargon) wrote:

    >tom_usenet <> wrote in message news:<>...
    >> On 22 Jul 2004 00:38:34 -0700, (Sargon) wrote:
    >>
    >> >Hi
    >> >
    >> >I have a problem with C++ traits. The following lil program provides a
    >> >Wrapper class which contains 3 static methods. (1 normal, 2 as a
    >> >template)

    >>
    >> No it doesn't, your code contains no "Wrapper" class at all!

    >
    >Oh my... I mistakenly posted the modified code... here's the
    >correct one:
    >
    >==================================================
    >#include <iostream>
    >using namespace std;
    >
    >enum colour_t { White, Black };
    >
    >
    >class Wrapper
    >{
    > public:
    > static void f(void);
    > template <colour_t c> static void f(void);
    >};
    >
    >
    >void Wrapper::f(void)
    >{
    > cout << "Hello from colourless f" << endl;
    >}
    >
    >
    >template <colour_t c>
    >void Wrapper::f(void)
    >{
    > if(c == White) cout << "Hello from white f" << endl;
    > else cout << "Hello from black f" << endl;
    >}
    >
    >
    >int main(void)
    >{
    > Wrapper::f();
    > Wrapper::f<White>();
    > Wrapper::f<Black>();
    >
    > return 0;
    >}


    The code is fine so the problem is the compiler. I'd suggest working
    round the problem by defining the template member inside Wrapper. e.g.

    class Wrapper
    {
    public:
    static void f(void);
    template <colour_t c> static void f(void)
    {
    if(c == White) cout << "Hello from white f" << endl;
    else cout << "Hello from black f" << endl;
    }
    };

    You don't get any semantic benefit from defining template members
    outside the class definition anyway.

    Tom
    tom_usenet, Jul 22, 2004
    #5
    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. skscpp

    templates - container traits

    skscpp, Jul 3, 2003, in forum: C++
    Replies:
    0
    Views:
    700
    skscpp
    Jul 3, 2003
  2. Alexander Stippler

    allocator traits?

    Alexander Stippler, Sep 2, 2003, in forum: C++
    Replies:
    4
    Views:
    377
    Alexander Stippler
    Sep 3, 2003
  3. Replies:
    3
    Views:
    1,317
    JH Trauntvein
    Mar 4, 2006
  4. ray
    Replies:
    1
    Views:
    1,298
    Robert Kern
    Jun 4, 2010
  5. Robert Nicholson
    Replies:
    0
    Views:
    106
    Robert Nicholson
    Jan 6, 2007
Loading...

Share This Page