Problem with C++ traits under OSX

S

Sargon

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
 
D

Drew McCormack

Sargon said:
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
 
T

tom_usenet

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
 
S

Sargon

tom_usenet said:
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;
}
==================================================

 
T

tom_usenet

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top