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]

Possible C++ bug


Hello!

I've installed your C++ compiler g++ (about two hours ago) and when I was compiling my vector library I found something which could be a bug.
Look at this:

------------------------------------------------
template<typename T>
class Vect
{
  T val[2];
  
 public:
 
  Vect operator- () const { return Vect(-val[0], -val[1]); }
  friend Vect<T> operator- <>(const Vect<T>& a, const Vect<T>& b);
};

template<typename T>
inline Vect<T> operator- (const Vect<T>& a, const Vect<T>& b)
{
  return Vect<T>(a.val[0] - b.val[0], a.val[1] - b.val[1]);
}
int main()
{
  Vect<double> v;

  return 0;
}
------------------------------------------------

This code produces these error messages:

------------------------------------------------
bug1.cpp: In instantiation of `Vect<double>':
bug1.cpp:20:   instantiated from here
bug1.cpp:9: invalid use of undefined type `class Vect<double>'
bug1.cpp:10: forward declaration of `class Vect<double>'
bug1.cpp:9: confused by earlier errors, bailing out
------------------------------------------------

But when I define no unary minus operator OR I define the operator as a friend function like this:
------------------------------------------------
template<typename T>
class Vect
{
 ...

  friend Vect<T> operator- <>(const Vect<T>& a);  // unary minus
  
 ...
};

template<typename T>
inline Vect<T> operator- (const Vect<T>& a)
{
  return Vect<T>(-a.val[0], -a.val[1]);
}

 ...
------------------------------------------------

everything is OK.
What is wrong with 'friend Vect<T> operator- <>( ... '? Is this correct? Am I wrong? (In this case I am sorry for wasting your time!)

Send me, please, your answer.

Facts about my computer:

OS: RedHat Linux 6.1
Hardware:  Intel Pentium 100 MHz, 40 MB EDO RAM
GCC: gcc version 2.95.2 19991024 (release)
     (old gcc used to compile this one was gcc version egcs-2.91.66 19990314/Lin
     ux (egcs-1.1.2 release))
     
     Compiled with the new library libstdc++-2.90.8.
     File srcdir/gcc/cp/decl2.c was changed to make flag honor-std implicitly on
     
     Installation procedure:
       srcdir/configure --prefix=/usr/local/gcc-2.95.2 --enable-shared --enable-
       languages=c++
       
       make CFLAGS='-O2' LIBCFLAGS='-g -O2' LIBCXXFLAGS='-g -O2 -fno-implicit-te
       mplates' bootstrap-lean
       
       make install
     
     File bug1.cpp compiled as:
       g++ -c bug1.cpp
       
       
        Martin Benda				mbenda@students.zcu.cz



--== Sent via Deja.com http://www.deja.com/ ==--
Before you buy.

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