delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/01/27/06:38:54

Date: Thu, 27 Jan 1994 12:17:39 +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


Hi,

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

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

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