Mail Archives: djgpp/1995/02/07/13:07:58
Hi anybody,
I've an simple-looking error linking from multiple modules each using the
same instance of a template-class. In the example below 'main' and 'sub'
uses class 'store<int>' declared in file 'temp.h', both compile ok.
Linking the files produces the 'multiple definition'-error as you can see
in the appended file 'ERR.LOG' produced by redirecting the output of make.
Can anybody tell me how to correct this error?
Thanks, Roland
+---------------------------------------+---------------------------+
I Roland Exler I EMAIL: I
I Universitaet Linz I R DOT Exler AT jk DOT uni-linz DOT ac DOT at I
I Institut fuer Elektrische Messtechnik I I
I Altenbergerstr. 69 I Phone: I
I A-4040 Linz, AUSTRIA I + 43 732 2468 9205 I
+---------------------------------------+---------------------------+
************************ File TEMP.H ********************************
#ifndef _TEMP_H
#define _TEMP_H
template <class TYPE>
class store
{
protected:
TYPE data;
public:
store(TYPE daten) {data=daten;};
TYPE read();
};
template <class TYPE>
TYPE store<TYPE>::read()
{ return data; }
#endif
************************ File MAIN.CC ********************************
#include<iostream.h>
#include "temp.h"
void sub();
int main()
{
store<int> one(1);
store<int> two(2);
cout<<"main: "<<one.read()+two.read()<<endl;
sub();
return 0;
}
************************ File SUB.CC ********************************
#include<iostream.h>
#include "temp.h"
void sub()
{
store<int> one(1);
store<int> two(2);
cout<<"sub: "<<one.read()+two.read()<<endl;
}
************************ MAKEFILE ********************************
.cc.o:
gcc -Wall -O2 -m486 -c $<
.o:
gcc -Wall -O2 -m486 -o $@ $^ -lgpp
main: main.o sub.o
main.o: main.cc temp.h
sub.o: sub.cc temp.h
************************ File ERR.LOG ********************************
gcc -Wall -O2 -m486 -c main.cc
gcc -Wall -O2 -m486 -c sub.cc
gcc -Wall -O2 -m486 -o main main.o sub.o -lgpp
sub.cc(.text+0x64): multiple definition of `store<int>::read(void)'
main.cc(.text+0x70): first defined here
make.exe: *** [main] Error 1
- Raw text -