This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

inheritance and templates


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]