Results 1 to 6 of 6

Thread: Problem of C++ Template

  1. #1
    Join Date
    Nov 2009
    Posts
    37

    Problem of C++ Template

    I have a class fraction (part of a maths package) that I spend in the Template, it is called now FractionG. So I normally made all the changes but adequate unit testing of my overload =, ==,! = does not work (using the example on my MatriceG). I have no error except "template class has already been defined" to build

    FractionG_2.cpp following code:
    Code:
    #ifndef _FractionG
    #include "FractionG.h"
    #endif
    template <class G>
    FractionG <G> &FractionG <G>::operator =(const FractionG& f) {
    m_numerator= f.m_numerator;
    m_denominator= f.m_denominator;
    return *this ;
    }
    template <class G>
    FractionG <G> &FractionG <G>::operator =(int op) {
    m_numerator= op ;
    m_denominator= 1 ;
    return *this ;
    }
    template <class G>
    bool FractionG <G>::operator ==(const FractionG& f) {
       return numerator()*f.denominator() == f.numerator()*denominator(); 
    }
    integer
    template <class G>
    bool FractionG <G>::operator ==(int op) {
       return numerator() == op*denominator();
    }
    template <class G>
    bool FractionG <G>::operator !=(const FractionG& f) {
    return numerator()*f.denominator() != f.numerator()*denominator();
    }
    integer
    template <class G>
    bool FractionG <G>::operator !=(int op) {
       return numerator() != op*denominator();
    }
    And here's my unit test:
    Code:
    #ifndef _FractionG_
    #include "..\_SRC\FractionG.h"
    #endif
    #include "..\_SRC\FractionG.cpp"
    #include "..\_SRC\FractionG_2.cpp"
    #include "T:\_Tests\Tests\Tests.h"
    void main () {
       Tests::Begin("_Maths.FractionG", "2.0.0" );
          Tests::Design("Control of operators (First part)", 3);
             /*Tests::Case("Operator == : comparison of two FractionG" ); {
             
         FractionG f1(3,3);
      Fraction f2(5), f3(-3), f4(6);
      int f5(2,1), f6(12,6), f7(3,1), f8(-3,1);
      int f9(10,5), f10(-10,-5);
                Tests::Unit(0,  f1==f2);
                Tests::Unit(1,  f3==f4);
                Tests::Unit(1,  f5==f6);
                Tests::Unit(0,  f7==f8);
                Tests::Unit(0,  f9==f10);
      Tests::Unit(1,  f2==f9);
             }*/
             Tests::Case("Operator == : comparison with an integer" ); {
      int f1(2),  f2(-2), f3(0), f4(0);
                Tests::Unit(1,  f1==2);
                Tests::Unit(1,  f2==-2);
                Tests::Unit(1,  f3==0);
                Tests::Unit(0,  f4==-1);
             }
             /*Tests::Case("Operator != : comparison of two FractionGs" ); {
      int f1(2),  f2(7), f3(-2), f4(2);
      int f5(2), f6(-4), f7(1), f8(1);
                Tests::Unit(1,  f1!=f2);
                Tests::Unit(1,  f3!=f4);
      Tests::Unit(0,  f4!=f5);
                Tests::Unit(1,  f5!=f6);
                Tests::Unit(0,  f7!=f8);
             }*/
         
             Tests::Case("Operator != : comparison with an integer" ); {
      int f1(2),  f2(4), f3(-1), f4(-1);
                Tests::Unit(0,  f1!=2);
                Tests::Unit(1,  f2!=-2);
                Tests::Unit(1,  f3!=0);
                Tests::Unit(0,  f4!=-1);
             }
          Tests::Design("Control of operators (Second part)", 3);
             /*Tests::Case("Operator = : case of two FractionGs" ); {
             FractionG& <FractionG> f1(2,3),  f2(5,4);
             FractionG& <FractionG> f(0);
                f=f1;  Tests::Unit(1, f==f1);
                       Tests::Unit(0, f==f2);
                f1=f2; Tests::Unit(0, f1==f);
                       Tests::Unit(1, f1==f2);
             }
             Tests::Case("Operator = : case of an integer" ); {
             FractionG& <FractionG> f(-1);
                f=0; Tests::Unit(0, f==1);
                     Tests::Unit(1, f==0);
                     Tests::Unit(1, f==-0);
                f=7; Tests::Unit(0, f==0);
                     Tests::Unit(1, f==7);
             }
    */
       Tests::End();
    }

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem of C++ Template

    Its normal the return <G> line 18? I have doubt if there is some problem over here. And we must not put the body of template in a .cpp but one .h.

  3. #3
    Join Date
    Nov 2009
    Posts
    37

    Re: Problem of C++ Template

    Here in .H, overloading the constructor works.

    Code:
    #include <stdio.h>
    #include <math.h>
    #ifndef _Object_
    #include "T:\_Archives\Object\Object.h"
    #endif
    #ifndef _Archive_
    #include "T:\_Archives\Archive\Archive.h"
    #endif
    #ifndef _Archive_
    #include "T:\_Archives\_Archives\_Archives.h"
    #endif
    #define _FractionG_
    int PGCD (int, int);
    int PPCM (int, int);
    template <class G> class FractionG {
    private :
    int m_numerator;
    int m_denominator;
    void  reduce();
    public :
    FractionG () {m_numerator= 0; m_denominator=1;}
    FractionG(int, int);
    FractionG(int);
    FractionG(const FractionG& );
    inline bool ok()  const {return (m_denominator==0 ? false : true);}
    inline bool nok() const {return !ok();}
    inline int numerator()   const {return m_numerator;}
    inline int denominator() const {return m_denominator;}
    FractionG& operator =(const FractionG& );
    FractionG& operator =(int);
    FractionG* operator +(const FractionG& );
    FractionG* operator +(int);
    FractionG& operator +=(const FractionG& );
    FractionG& operator +=(int);
    FractionG* operator -(const FractionG& );
    FractionG* operator -(int);
    FractionG& operator -=(const FractionG& );
    FractionG& operator -=(int);
    FractionG* operator *(const FractionG& );
    FractionG* operator *(int);
    FractionG& operator *=(const FractionG& );
    FractionG& operator *=(int);
    FractionG* operator /(const FractionG& );
    FractionG* operator /(int);
    FractionG& operator /=(const FractionG& );
    FractionG& operator /=(int);
    bool operator ==(const FractionG& );
    bool operator ==(int);
    bool operator !=(const FractionG& );
    bool operator !=(int);
    bool operator <(const FractionG& );
    bool operator <(int);
    bool operator <=(const FractionG& );
    bool operator <=(int);
    bool operator >(const FractionG& );
    bool operator >(int);
    bool operator >=(const FractionG& );
    bool operator >=(int);
    char* toString() const;
    };

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem of C++ Template

    What I mean is that except in special cases, we see the power of definition templates when used. So we place the definition in the .hpp - directly or indirectly, I tend to put one .tpp is included in the .hpp, like my inline definitions are sometimes in one .ipp.

    Second remark, using absolute paths in the includes is a bad idea. Use relative paths (and with/as separator, it works on Windows as on Unix) and shows the roots or find your compiler.

  5. #5
    Join Date
    Nov 2009
    Posts
    37

    Re: Problem of C++ Template

    For paths it is normal, we all work on the same basis T: But it's true that you're right! Otherwise I do not see where there is a problem, do I emulate the MatriceG fraction that works very well (except the persistence).

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem of C++ Template

    So there's going to go very slowly

    Property:
    Code:
    // template.hpp
    template<class T> struct foo
    {
       void bar() { /* some code; */ }
    };
    Although NOT:
    Code:
    // template.hpp
    template<class T> struct foo
    {
       void bar();
    };
    Code:
    //template.cpp
    #include "template.hpp"
    template<class T> void foo<T>::bar() { /* some code; */ }

Similar Threads

  1. Replies: 2
    Last Post: 28-01-2012, 03:50 PM
  2. Word template problem
    By AMISH in forum Windows Software
    Replies: 4
    Last Post: 27-08-2010, 01:05 PM
  3. How to create a template of template
    By Harmony60 in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 05:17 PM
  4. Problem of template and delete function
    By Hajra in forum Software Development
    Replies: 4
    Last Post: 20-11-2009, 08:12 PM
  5. Replies: 1
    Last Post: 20-05-2008, 09:24 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,553,262.09543 seconds with 17 queries