www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/11/21/15:30:14

Xref: news-dnh.mv.net comp.os.msdos.djgpp:3360
Path: news-dnh.mv.net!mv!barney.gvi.net!news.sprintlink.net!news.uoregon.edu!mayonews.mayo.edu!newsdist.tc.umn.edu!umn.edu!spool.mu.edu!pravda.aa.msen.com!simtel!news.kei.com!newsfeed.internetmci.com!in2.uu.net!news.usa.net!news.usa.net!not-for-mail
From: heintze AT usa DOT net (Siegfried Heintze)
Newsgroups: comp.os.msdos.djgpp
Subject: Multiple definition of function with templates
Date: 16 Nov 1995 01:12:23 -0700
Organization: Internet Express (800-592-1240 customer service)
Lines: 62
Nntp-Posting-Host: earth.usa.net
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Dj-Gateway: from newsgroup comp.os.msdos.djgpp

I have a problem with template programs. Consider the attached files when 
compiled and linked:

gcc -c func.cc
gcc -c main.cc
gcc func.o main.o -lgpp
func.cc(.text+0x42): multiple definition of `Array<float, 5>::operator[](int)'
main.cc(.text+0x4c): first defined here

How do I instantiate a template and link it only once? Other DOS and
UNIX linkers throw out redundant function bodies generated by template
functions and template classes, but apparently the GNU one does not.

   Thanks,
                       Sieg
-------------------------------------------------------------------

// array.h
#ifndef ARRAY
#define ARRAY

template<class T, int S>
class Array{
public:
	T& operator[](int i);
private:
	T _data[S];
};

#endif

// array.cc
#include "array.h"
template<class T, int S>
T& Array<T,S>::operator[](int i) { return _data[i]; }

// func.cc
#include <iostream.h>
#include "array.cc"
void func(){
	Array<float, 5> x;
	cout << "function func()"<<endl;
	return;
}

// main.cc
#include <iostream.h>
#include "array.cc"
void func();
main(){
	cout << "Begin"<<endl;
	Array<float, 5> x;
	func();
	return 0;
}

-- 
-------------------------------------------------------------------------------
Siegfried Heintze                             heintze AT usa DOT net
3600 Berkley Avenue                           72740 DOT 1365 AT compuserve DOT com
Boulder, CO 80303                             Voice: 303 499 6575

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019