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::conv ert. If I write
convert as a member function instead, it all works fine. The whole
Numerical::Vect or 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? 2 1764
"Martin Magnusson" <martin@-xx-blecket-xx-.org> wrote in message
news:1095412402 .4V9RMbK7zaKQ2z dOMce88Q@terane ws... 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::conv ert. If I write convert as a member function instead, it all works fine. The whole Numerical::Vect or 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
In article <2q************ *@uni-berlin.de>,
John Harrison <jo************ *@hotmail.com> wrote: "Martin Magnusson" <martin@-xx-blecket-xx-.org> wrote in message news:109541240 2.4V9RMbK7zaKQ2 zdOMce88Q@teran ews... 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::conv ert. If I write convert as a member function instead, it all works fine. The whole Numerical::Vect or 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.
Alternativel y 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
ifndef/define directives, the code between the statements
define and endif was only compiled once. But this seems
to be incorrect, as I get a whole bunch of errors when
I compile the critter.
For example if I include header.h more than once the
|
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
variable for the multiple definitions. C++, however, does not permit
redefinition of a variable or any other entity for a very definite reason
that we will discuss later." Chapter 1. C++ Tutorial. Coronado Enterprises
Charles L
|
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 in the format
that I need it in order to create the reports that we use. So far this
has proven to be successful for the reports that I am doing and the
data that I am pulling into it. I just have one challenge that may
require a lot of work and I...
|
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 another project with
including "lib.lib". However, it gives me the errors: symbol xxx is
defined multiple times in a.obj and b.obj in "lib.lib". It seems that
there exists conflicts as I include some common ".h" file in both
source codes of the...
|
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 in all other files containing funktions,
when I compile I get this error multiple definition of `fVar'
why id that? I have only defined it one in include file?
--
| |
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. Only
when I initialize xxxx to two different values in A.c and B.c
respectively do I get a 'multiple definitions' error. I was under the
impression that I would get such an error even without initializing
xxxx twice, simply because i am (ostensibly)...
|
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 definitions are translated
to "weak" symbols, in which case multiple definitions are merged by the
linker, while some become "normal" symbols, triggering linker errors in
case they are defined more then once. Is it true that in C++, multiple
definitions...
|
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:
File a.h contains some constant definitions.
As a protection against multiple definitions of these constants I included the following lines at the top of it:
#ifndef const_file_def
#define const_file_def
and added at the end:
#endif
|
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 and defines a
few more elements. I use an entity to include base.dtd in a.dtd and
b.dtd. For example, in a.dtd:
<!ENTITY % include.base SYSTEM "base.dtd">
%include.base
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |