Connecting Tech Pros Worldwide Help | Site Map

Problem with C++ traits under OSX

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:45 PM
Sargon
Guest
 
Posts: n/a
Default Problem with C++ traits under OSX

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

  #2  
Old July 22nd, 2005, 04:45 PM
Drew McCormack
Guest
 
Posts: n/a
Default Re: Problem with C++ traits under OSX

Sargon wrote:
[color=blue]
> 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[/color]
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
  #3  
Old July 22nd, 2005, 04:45 PM
tom_usenet
Guest
 
Posts: n/a
Default Re: Problem with C++ traits under OSX

On 22 Jul 2004 00:38:34 -0700, sargon@gmail.com (Sargon) wrote:
[color=blue]
>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)[/color]

No it doesn't, your code contains no "Wrapper" class at all!
[color=blue]
>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.[/color]

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
  #4  
Old July 22nd, 2005, 04:45 PM
Sargon
Guest
 
Posts: n/a
Default Re: Problem with C++ traits under OSX

tom_usenet <tom_usenet@hotmail.com> wrote in message news:<q45vf0pr406iqt9hrafnpmi9i554pf5cjt@4ax.com>. ..[color=blue]
> On 22 Jul 2004 00:38:34 -0700, sargon@gmail.com (Sargon) wrote:
>[color=green]
> >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)[/color]
>
> No it doesn't, your code contains no "Wrapper" class at all![/color]

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;
}
==================================================

[color=blue][color=green]
> >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.[/color][/color]
  #5  
Old July 22nd, 2005, 04:45 PM
tom_usenet
Guest
 
Posts: n/a
Default Re: Problem with C++ traits under OSX

On 22 Jul 2004 06:19:55 -0700, sargon@gmail.com (Sargon) wrote:
[color=blue]
>tom_usenet <tom_usenet@hotmail.com> wrote in message news:<q45vf0pr406iqt9hrafnpmi9i554pf5cjt@4ax.com>. ..[color=green]
>> On 22 Jul 2004 00:38:34 -0700, sargon@gmail.com (Sargon) wrote:
>>[color=darkred]
>> >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)[/color]
>>
>> No it doesn't, your code contains no "Wrapper" class at all![/color]
>
>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;
>}[/color]

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.