Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiple class definitions

0 views
Skip to first unread message

Claus Gwiggner

unread,
Oct 20, 2000, 3:00:00 AM10/20/00
to
I cannot link two files declaring/defining the same class:

file1.h

class A{
public:
A();
}

file2.h
class A{
public:
A();
}


If I state the classes "extern", the linker (g++ 2.95.2) stops with "
`extern' can only be specified for objects and functions", if I state
the constructors "extern", the error message is "storage class
specified for field `A' ".

What is wrong ?

Thanks,

Claus

(The context of this multiple definition is a program written for
multiple operating systems with #ifdef ... constructs )


Dave Korn

unread,
Oct 20, 2000, 3:00:00 AM10/20/00
to
Claus Gwiggner wrote in message
<39F01865...@informatik.uni-muenchen.de>...

>I cannot link two files declaring/defining the same class:
>
>file1.h
>
>class A{
>public:
> A();
>}
>
>file2.h
>class A{
>public:
> A();
>}

Sorry, that's not two files declaring the same class. That's two files
defining two different classes (that happen to have the same contents)
and trying to use the same name for these two different classes. Now,
you can redefine a #define without an error if you redefine it exactly
the same as it was before, but you aren't allowed to redeclare classes,
structures, unions, enums, typedefs etc. That's just part of the language:
only one definition of each class.

You should extract the class definition into a file of its own and
#include it from both your source files independently

DaveK
--
They laughed at Galileo. They laughed at Copernicus. They laughed at
Columbus. But remember, they also laughed at Bozo the Clown.

Kaz Kylheku

unread,
Oct 20, 2000, 3:00:00 AM10/20/00
to
On Fri, 20 Oct 2000 12:03:17 +0200, Claus Gwiggner
<gwig...@informatik.uni-muenchen.de> wrote:
>I cannot link two files declaring/defining the same class:
>
>file1.h
>
>class A{
>public:
> A();
>}
>
>file2.h
>class A{
>public:
> A();
>}

These are header files; one does not link headers. Rather, they get included
in translation units which are then translated and linked.

If you include both of the above headers in the same translation unit, you then
have an invalid redeclaration of a class, which will result in a compile time
error.

I'm assuming that the classes are unrelated and that this is a case of an
accidental name clash. In that case you have worse problems than a mere
duplicate declaration; you then have violations of the ODR (one definition
rule) by having multiple definitions of constructors, destructors, other
member functions and possibly clashing static data members. Even if
the headers are never included in the same translation unit, there will
be a link problem.

You are going to have to rename one of the classes, or use namespaces.
There is no way to defeat the One Definition Rule of C++.

--
Any hyperlinks appearing in this article were inserted by the unscrupulous
operators of a Usenet-to-web gateway, without obtaining the proper permission
of the author, who does not endorse any of the linked-to products or services.

0 new messages