Mail Archives: djgpp/1994/01/27/09:12:24

From: <cichosz AT ii DOT pw DOT EDU DOT PL> (Pawel Cichosz)
Subject: Using templates with DJGPP
Organization: Kaleida Labs, Inc.
Originator: daemon AT talisman DOT kaleida DOT com
Date: 27 Jan 94 11:17:39 GMT
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Reply-To: djgpp AT sun DOT soe DOT clarkson DOT edu


My problem is G++ problem rather than just DJGPP problem, but I am
sure some of DJGPP users will be able to help me.

I have just installed DJGPP on my system and tried to use it. I have 
problems with C++ programs using templates. Consider the following 
example, as simple as possible.

File dummy.h:

#include <iostream.h>

template <class T>
class Dummy
  private :

    T t;

  public :

    Dummy (const T &t) : t (t) {}

    friend istream & operator >> (istream &, Dummy <T> &);
    friend ostream & operator << (ostream &, const Dummy <T> &);

template <class T>
istream & operator >> (istream &is, Dummy <T> &d)
  return is >> d.t;

template <class T>
ostream & operator << (ostream &os, const Dummy <T> &d)
  return os << d.t;


#include "dummy.h"

int main (void)
  Dummy <int> d (999);

  cin >> d;
  cout << d;

  return 0;


gcc -o main -lgpp 

I get a linker message about undefined symbol
'istream & operator >> (istream &, Dummy <int> &)' (as a matter of fact,
ld reports this name in an encoded form), while the other operator (<<) is
defined. I looked into cc1plus's assembler output and found that in fact
one operator function was generated and the other was not. I suppose (and 
I hope !) that I am doing something wrong. Any ideas ?

I would also appreciate a more general advice on how to use templates 
with DJGPP (G++) in an efficient and portable way. I have source files
that make an extensive use of templates, which compiled and linked 
successfully with BC++ 3.1.

I hope I am not bothering you too much.



P.S. When I define both operators inside class declaration, it works. 
Also, when I change the second parameter's type for operator >> () adding

istream & operator >> (istream &, const Dummy <int> &)

(this makes its definition illegal, but the compiler produces only 
a warning), it works. Isn't that strange ?

P.S.(2) (Perhaps more relevant to DJGPP itself)
When I remove #include <iostream.h> directive from dummy.h, I get
a segmentation violation. But perhaps something is wrong in my 

- Raw text -

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