Multiple definitions

M

Martin Magnusson

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?
 
J

John Harrison

Martin Magnusson said:
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
 
G

Greg Comeau

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top