delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/02/01/05:32:25

Date: Tue, 1 Feb 1994 11:16:32 +0100
From: "Pawel Cichosz" <cichosz AT ii DOT pw DOT edu DOT pl>
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Using templates with DJGPP - again


Hi,

As I noticed, there is some discussion going on concerning templates with
G++. That is why I dare to repost my previous message (have you received
it ?), still unanswered, where I reported my troubles compiling templates
with DJGPP. I believe this time someone will be so kind as to help me.

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;
}

--------------------------------------------
File main.cc:
--------------------------------------------

#include "dummy.h"

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

  cin >> d;
  cout << d;

  return 0;
}


Running 

gcc -o main main.cc -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, probably something obvious. 
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 hope I am not bothering you too much. Looking forward to your valuable
suggestions.

Regards,

Pawel

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

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 
configuration.



- Raw text -


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