473,322 Members | 1,259 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,322 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 1742

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: prettysmurfed | last post by:
Hi all I have this, probably stupid question, how to avoid multiple definitions when a header file is included more than once. I thought, when you wrote the header-file and used the...
5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
4
by: JackyMove | last post by:
Dear all, I have encount the following problem. I have compiled a library "lib.lib" successfully using a simulator compatable to VC++ on Windows platform. Then I try to build an executatble in...
14
by: Carramba | last post by:
hi! I have program with several funktion witch are in separete files, I have one include file were I have definet some variables and initiated 'const double fVar=0.874532;' this files is includet...
9
by: lbj137 | last post by:
I have two files: A.c and B.c. In both files I define a global variable, int xxxx; When I compile with a green hills compiler (and also i think with a GNU compiler) I get no errors or warnings....
8
by: yossi.kreinin | last post by:
Hi! When are multiple definitions of global variables with the same name considered legal in C, and how is it different from C++? It appears that in terms of assembly language, some C...
6
by: javan | last post by:
Hello, I am new to C++, coming from a background in Java and procedural languages. I am currently translating some code from Java to C++ as an exercise and have run into the following problem: ...
4
by: junyang | last post by:
Hi all, I have one DTD fragment, base.dtd, that contains a bunch of useful element definitions (say an element named "base"), and two DTD fragments, a.dtd and b.dtd, that each build on base.dtd...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.