help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Question about template compilered by your gpp


From: Y LIN
Subject: Question about template compilered by your gpp
Date: Tue, 17 Oct 2000 13:18:21 -0700 (PDT)

  Hi,
 
  I am a student. I have a question about your gpp
  (C++ compiler) on a PC. I compiler a simple program
  listed below and your gpp shows:
 

------------------------------------------------------
 C:\>gpp test.cpp -lm
 tt2.cpp:22: warning: friend declaration `void
 Swap(A<DType> &, A<DType> &)'
 tt2.cpp:22: warning:   declares a non-template
 function
 tt2.cpp:22: warning:   (if this is not what you
 intended, make sure
 tt2.cpp:22: warning:   the function template has
 already been declared,
 tt2.cpp:22: warning:   and add <> after the function
 name here)
 tt2.cpp:22: warning:   -Wno-non-template-friend
 disables this warning.
 tt2.cpp:23: warning: friend declaration `class
 A<DType> operator +(A<DType> &, A
 <DType> &)'
 tt2.cpp:23: warning:   declares a non-template
 function
 c:/djgpp/tmp\ccrO3NCR.o(.text+0x267):tt2.cpp:
 undefined reference to `Swap(A<dou
 ble> &, A<double> &)'
 c:/djgpp/tmp\ccrO3NCR.o(.text+0x27a):tt2.cpp:
 undefined reference to `Swap(A<flo
 at> &, A<float> &)'
 c:/djgpp/tmp\ccrO3NCR.o(.text+0x28d):tt2.cpp:
 undefined reference to `Swap(A<int
 > &, A<int> &)'
 collect2: ld returned 1 exit status

------------------------------------------------------
 
  It seems that my program has nothing error!
  Could you tell me what should I do?
  Thank you.
 
 -----------------------------------------------------
 program: test.cpp
 
 #include "iostream.h"
 
 template <class DType>
 class A
 {
    private:
       DType E;
 
    public:
       A() {};
       A(DType vd)
       {
          E = vd;
       };
       ~A() {};
       DType Output()
       {
          return E;
       };
 
       friend void Swap(A<DType> &va1, A<DType>
 &va2);
       friend A<DType> operator +(A<DType> &va1,
 A<DType> &va2);
 };
 
 // I want to separate the header (above) and the
 // code (below)
 template <class DType>
 void Swap(A<DType> &va1, A<DType> &va2)
 {
    DType dttmp;
 
    dttmp = va1.E;
    va1.E = va2.E;
    va2.E = dttmp;
 }
 
 template <class DType>
 A<DType> operator +(A<DType> &va1, A<DType> &va2)
 {
    A<DType> atmp;
 
    atmp.E = va1.E + va2.E;
    return atmp;
 }
 
 int main()
 {
    A<double> da1(1.), da2(2.);
    A<float> fa1(-10.F), fa2(-20.F);
    A<int> ia1(100), ia2(200);
 
    cout << "da1 = " << da1.Output() << ", da2 = " <<
 da2.Output() << endl <<
       "fa1 = " << fa1.Output() << ", fa2 = " <<
 fa2.Output() << endl <<
       "ia1 = " << ia1.Output() << ", ia2 = " <<
 ia2.Output() << endl;
    Swap(da1, da2);
    Swap(fa1, fa2);
    Swap(ia1, ia2);
    cout << "After Swap:" << endl;
    cout << "da1 = " << da1.Output() << ", da2 = " <<
 da2.Output() << endl <<
       "fa1 = " << fa1.Output() << ", fa2 = " <<
 fa2.Output() << endl <<
       "ia1 = " << ia1.Output() << ", ia2 = " <<
 ia2.Output() << endl;
 
    return 0;
 }
 

__________________________________________________
Do You Yahoo!?
Yahoo! Messenger - Talk while you surf!  It's FREE.
http://im.yahoo.com/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]