473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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
Jul 22 '05 #1
4 1288
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
Jul 22 '05 #2
On 22 Jul 2004 00:38:34 -0700, sa****@gmail.com (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
Jul 22 '05 #3
tom_usenet <to********@hotmail.com> wrote in message news:<q4********************************@4ax.com>. ..
On 22 Jul 2004 00:38:34 -0700, sa****@gmail.com (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.

Jul 22 '05 #4
On 22 Jul 2004 06:19:55 -0700, sa****@gmail.com (Sargon) wrote:
tom_usenet <to********@hotmail.com> wrote in message news:<q4********************************@4ax.com>. ..
On 22 Jul 2004 00:38:34 -0700, sa****@gmail.com (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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donald 'Paddy' McCarthy | last post by:
Hi, I am having a few problems with a GUI. I am new to traits and wxGlade. I have used wxGlade to create a Form with an embedded space for a CustomWidget. I have the traits demo and would like...
0
by: skscpp | last post by:
What's wrong with the following code? Compiler error is at the bottom. The compiler I am using is gcc version 2.95. ============================= // traits.h =============================...
12
by: Mark A. Gibbs | last post by:
Good day, What is the safest way to get a non-end of file value? char_traits<char_type>::eof() will get me eof, but how can I safely and consistently generate a value that is not eof? should i...
8
by: Peter Collingbourne | last post by:
Hello I am trying to do some template metaprogramming involving enabling or disabling a method in a parameterised class based on some condition. I am trying to use the Boost enable_if mechanism...
4
by: Martin MacRobert | last post by:
Hi Gang, The following code does not compile, but I can't figure out why. The compiler complains that the CuriouslyDerivedType (CRDerived) does not publish the "value_type", yet in fact the class...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
9
by: Bit Byte | last post by:
Can't seem to get my head around the point of a trait class - no matter how many times I read up on it - why not simply use functors or function pointers ? Anyone care to explain this in a...
2
by: Milburn Young | last post by:
I see the STL using class templates accepting a traits and a policy. To me, the traits are the "what" and the policy is the "how". How can a policy know what to do without the traits of the topic?...
0
by: Milburn Young | last post by:
I see the STL using class templates accepting a traits and a policy. To me, the traits are the "what" and the policy is the "how". How can a policy know what to do without the traits of the topic?...
2
by: Colin J. Williams | last post by:
Using >easy_install -v -f http://code.enthought.com/enstaller/eggs/source enthought.traits The result is: .... many lines ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.