Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > multiple definition

Reply
Thread Tools

multiple definition

 
 
Samuele Armondi
Guest
Posts: n/a
 
      06-30-2003

"Jochen Zeischka" <> wrote in message
news:bdrv8k$nki$...
> Hi everybody!
>
> I have a question concerning code organisation. Suppose I have the

following
> header file:
>
> #ifndef SOME_NAME
> #define SOME_NAME
>
> namespace N {
> void F()
>
> ... here comes the implementation
> }
> }
>
> #endif
>
> In this case I would think that you never ever can encounter a 'multiple
> definition' problem because
> 1) SOME_NAME is defined the first time F is defined
> 2) there is only one definition of F, so it even wouldn't be a problem

to
> define the same F a hundred times...
>
> Still, I get the error message that F is multiply defined when this header
> file is used in other header files. (And there is definitely not another

'F'
> defined in any of these header files)
>
> Can anyone help me out?
>
> Thanks!
>
> Jochen
>
>

I had the same problem. I solved it by splitting the code into two: the
declarations in a .h file and _all_ the implementations in a .cpp file, i.e.
// foo.h
#ifndef foo_h
#define foo_h

namespace n
{
class bar
{
private:
int i;
public:
bar(int);
void f();
};
}
#endif

//foo.cpp
n::bar::bar(int n) : i(n)
{
}

void n::bar::f()
{
//whatever
}

HTH,
S. Armondi



 
Reply With Quote
 
 
 
 
Jochen Zeischka
Guest
Posts: n/a
 
      07-01-2003
Hi everybody!

I have a question concerning code organisation. Suppose I have the following
header file:

#ifndef SOME_NAME
#define SOME_NAME

namespace N {
void F()

... here comes the implementation
}
}

#endif

In this case I would think that you never ever can encounter a 'multiple
definition' problem because
1) SOME_NAME is defined the first time F is defined
2) there is only one definition of F, so it even wouldn't be a problem to
define the same F a hundred times...

Still, I get the error message that F is multiply defined when this header
file is used in other header files. (And there is definitely not another 'F'
defined in any of these header files)

Can anyone help me out?

Thanks!

Jochen


 
Reply With Quote
 
 
 
 
Josephine Schafer
Guest
Posts: n/a
 
      07-01-2003

"Jochen Zeischka" <> wrote in message
news:bdrv8k$nki$...
> Hi everybody!
>
> I have a question concerning code organisation. Suppose I have the

following
> header file:
>
> #ifndef SOME_NAME
> #define SOME_NAME
>
> namespace N {
> void F()
>
> ... here comes the implementation
> }
> }
>
> #endif
>
> In this case I would think that you never ever can encounter a 'multiple
> definition' problem because
> 1) SOME_NAME is defined the first time F is defined
> 2) there is only one definition of F, so it even wouldn't be a problem

to
> define the same F a hundred times...
>
> Still, I get the error message that F is multiply defined when this header
> file is used in other header files. (And there is definitely not another

'F'
> defined in any of these header files)
>
> Can anyone help me out?
>
> Thanks!
>
> Jochen


Please note that the header guards protect multiple inclusion of a header
file in
a single translation unit(e.g.cpp file). If you happen to include this
header file in several
translation units then the linker problem will arise. Either move the
definition to some implementation file or
else make the function inline(only if it suits in your case) .





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
How to avoid multiple definition of a variable by multiple inclusion of a header file lars.uffmann@rwth-aachen.de C++ 11 08-05-2006 10:24 PM
Automagic determination of definition based on definition location. Jon Slaughter C++ 4 10-26-2005 05:00 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57