This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Template Multiple-defs/Repo bug



The following test case succeeds if it is all compiled as a single
file but fails in  the link with a template multiply defined message
when broken out into one header and two .cc files.  The generated
message is:

g++ -g -O2 sillytest.cc sillytest2.cc
/tmp/ccG0sZvP.o: In function `Rectangle<unsigned short> Default::defGeo<Sub>(Sub const &)':
/export/home/josh/src/VAG/X11/sillytest.h:63: multiple definition of 
`Rectangle<unsigned short> Default::defGeo<Sub>(Sub const &)'
/tmp/ccMOvXOk.o:/export/home/josh/src/VAG/X11/sillytest.h:63: first defined here
collect2: ld returned 1 exit status

This was with the Aug. 03, `98 snapshot on a linux  i686-pc-linux-gnu.



Here are the three files that cause the failure:

/***************** begin sillytest.h ********************/


template <class T>
class Rectangle {
protected:
  T d_x;
  T d_y;
  T d_xLen;
  T d_yLen;
public:
  Rectangle() throw() : d_x(0), d_y(0), d_xLen(0), d_yLen(0) {}
  Rectangle(T x, T y, T xlen, T ylen);
  T xLength() const throw() { return d_xLen; }
  T yLength() const throw() { return d_yLen; }
  T area() const throw() { return xLength()*yLength(); }
  T xCoord() const throw() { return d_x; }
  T yCoord() const throw() { return d_y; }
  void getCenter(float points[2]) const throw();
  void setXLength(const T& val) throw() { d_xLen = val; }
  void setYLength(const T& val) throw() { d_yLen = val; }


}; // Rectangle

template <class T>
inline
Rectangle<T>::Rectangle(T x, T y, T xlen, T ylen)  
  : d_x(x), d_y(y), d_xLen(xlen), d_yLen(ylen) 
{
}

template <class T>
inline
void Rectangle<T>::getCenter(float points[2]) const throw()
{
  points[0] = xCoord() + xLength()/2;
  points[1] = yCoord() + yLength()/2;
}

typedef unsigned short ushort;

class Default {
public:
  template <class T>
  static Rectangle<ushort> defGeo(const T& arg) throw() { return Rectangle<ushort>(1,1,1,1);
}
};



struct Super {
  typedef Rectangle<ushort> Rect;

  Rect d_r;

  Super();
};

struct Sub : public Super {
  int i;
};

template <>
Rectangle<ushort> Default::defGeo<Sub>(const Sub& arg) throw() { return
Rectangle<ushort>(10,10,10,10); }


/******************* end sillytest.h **************************************/

/****************** begin sillytest.cc **********************************/

#include "sillytest.h"

int main(int argc, char** argv)
{
  Super r;
  Sub s;

}

/************************ end sillytest.cc *****************************/

/********************** begin sillytest2.cc ****************************/

#include "sillytest.h"

Super::Super() : d_r(Default::defGeo(*this)) {}

/********************** end sillytest2.cc *****************************/



- Josh

jstern@citilink.com




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]