* inheritance and templates
@ 1998-03-18 22:03 Dimitri PAPADOPOULOS-ORFANOS
0 siblings, 0 replies; only message in thread
From: Dimitri PAPADOPOULOS-ORFANOS @ 1998-03-18 22:03 UTC (permalink / raw)
To: egcs
Hi,
I am mixing inheritance and template member functions (templatized
constructors a la STL) in a program. I cannot get it to link properly.
Here is it:
// file A.h
// --------
#include <vector>
class A {
public:
template <class InputIterator> A(InputIterator, InputIterator);
virtual void foo() = 0;
protected:
vector<int> _data;
};
template <class InputIterator>
inline
A::A(InputIterator first, InputIterator last) : _data(first, last)
{
}
// file B.h
// --------
#include "A.h"
class B : public A {
public:
template <class InputIterator> B(InputIterator, InputIterator);
virtual void foo();
};
template <class InputIterator>
inline
B::B(InputIterator first, InputIterator last) : A(first, last) {
}
template <class InputIterator>
inline
B(InputIterator first, InputIterator last) : A(first, last) {
}
// file B.cc
// ---------
#include "B.h"
#include <iostream.h>
void B::foo() {
for (vector<int>::iterator i = _data.begin(); i != _data.end(); ++i) {
cout << *i << endl;
}
}
// file main.cc
// ------------
#include "B.h"
#include <list>
int main() {
list<int> l;
l.push_back(1); l.push_back(2); l.push_back(3);
B b(l.begin(), l.end());
A& a = b;
a.foo();
return 0;
}
I keep getting errors at link-time with GNU C++ 2.8.1 or EGCS:
$ g++ -o main main.cc B.cc
/tmp/cca268752.o(.rodata+0xc): multiple definition of `B virtual table'
/tmp/cca268751.o(.rodata+0x18): first defined here
/tmp/cca268752.o: In function `B type_info function':
/tmp/cca268752.o(.text+0x60): multiple definition of `B type_info function'
/tmp/cca268751.o(.text+0x138): first defined here
collect2: ld returned 1 exit status
Is the above code legal? Is there something that I should be careful about
not to get such link-time errors? Is this a problem with GNU C++?
Thank you,
--
Dimitri Papadopoulos
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~1998-03-18 22:03 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-03-18 22:03 inheritance and templates Dimitri PAPADOPOULOS-ORFANOS
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).