471,049 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Multiple definitions

I have a problem with multiple definitions that I can't quite straighten
out.

I have a templated class defined inside a namespace, and I want to
create a function in that namespace that works on a specific instance of
the templated class.

Like this:

// file Numerical.hpp:
namespace Numerical
{
template<int N>
class Vector { ... };

Vector<3> convert( Vector<3> v )
{ ... }

}

However, when doing it like that I get complaints from gcc, during
linking, about multiple definitions of Numerical::convert. If I write
convert as a member function instead, it all works fine. The whole
Numerical::Vector class is defined in the hpp file. I'm pretty sure I
don't have any multiple includes, and that all of my header files have
the proper #ifndef guards.

What could be the problem?
Jul 22 '05 #1
2 1597

"Martin Magnusson" <martin@-xx-blecket-xx-.org> wrote in message
news:1095412402.4V9RMbK7zaKQ2zdOMce88Q@teranews...
I have a problem with multiple definitions that I can't quite straighten
out.

I have a templated class defined inside a namespace, and I want to
create a function in that namespace that works on a specific instance of
the templated class.

Like this:

// file Numerical.hpp:
namespace Numerical
{
template<int N>
class Vector { ... };

Vector<3> convert( Vector<3> v )
{ ... }

}

However, when doing it like that I get complaints from gcc, during
linking, about multiple definitions of Numerical::convert. If I write
convert as a member function instead, it all works fine. The whole
Numerical::Vector class is defined in the hpp file. I'm pretty sure I
don't have any multiple includes, and that all of my header files have
the proper #ifndef guards.

What could be the problem?


convert is not a template function so if you want to put it in a header file
you must declare it inline. Include guards are not the issue. If you include
the header file in more than one source file you are going to get multiple
definitions of any non-templated non-inline function.

Alternatively move convert to a source file, and just leave the prototype in
the header file. This is probably what you should do.

john
Jul 22 '05 #2
In article <2q*************@uni-berlin.de>,
John Harrison <jo*************@hotmail.com> wrote:

"Martin Magnusson" <martin@-xx-blecket-xx-.org> wrote in message
news:1095412402.4V9RMbK7zaKQ2zdOMce88Q@teranews.. .
I have a problem with multiple definitions that I can't quite straighten
out.

I have a templated class defined inside a namespace, and I want to
create a function in that namespace that works on a specific instance of
the templated class.

Like this:

// file Numerical.hpp:
namespace Numerical
{
template<int N>
class Vector { ... };

Vector<3> convert( Vector<3> v )
{ ... }

}

However, when doing it like that I get complaints from gcc, during
linking, about multiple definitions of Numerical::convert. If I write
convert as a member function instead, it all works fine. The whole
Numerical::Vector class is defined in the hpp file. I'm pretty sure I
don't have any multiple includes, and that all of my header files have
the proper #ifndef guards.

What could be the problem?


convert is not a template function so if you want to put it in a header file
you must declare it inline. Include guards are not the issue. If you include
the header file in more than one source file you are going to get multiple
definitions of any non-templated non-inline function.

Alternatively move convert to a source file, and just leave the prototype in
the header file. This is probably what you should do.


Yup. I would suggest the OP get a copy of Stroustrup's
"The C++ Programming Language" (see http://www.comeaucomputing.com/booklist )
and see Chapter 9 regarding some throughts on program construction
and file organization.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by prettysmurfed | last post: by
4 posts views Thread by JackyMove | last post: by
14 posts views Thread by Carramba | last post: by
9 posts views Thread by lbj137 | last post: by

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.