CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2009
    Posts
    5

    defining a template class problem

    Hi,

    I have defined the following class:

    Code:
    template<class T,char var>
    class CGenPoly : public CList
    {
        
    public:
    	CGenPoly();
    	//genpoly(int num);
    	CGenPoly(T elem);
    	CGenPoly(const CGenPoly& p);
    	void SetCoef(int power,T elem);   //Omri
    	CGenPoly operator+ (const CGenPoly& p); 
    	CGenPoly operator* (const CGenPoly& p);
    	CGenPoly& operator+= (const CGenPoly& p);
    	CGenPoly& operator*= (const CGenPoly& p);    
    	CGenPoly& operator= (const CGenPoly& p);   //Omri
    	CGenPoly operator^ (int power);  // Omri
        CGenPoly operator* (const T& elem);
        CGenPoly operator+ (const T& elem);  //Omri  //poly + T
    	T operator() (T point);    
    	friend ostream& operator << <T,var>(ostream& os, const CGenPoly& p);  //Omri
    	friend CGenPoly operator * <T,var>(const T& elem, const CGenPoly& p);
           friend CGenPoly operator + <T,var>(const T& elem, const CGenPoly& p);  // Omri  //T + poly
    
    };
    all the methods are ok except of the last two. I get the following error about those two:
    genpoly.h(85) : error C2143: syntax error : missing ';' before '<'

    I can't find what I did wrong and I would appreaicate it if someone could help me.

    Thanks
    Last edited by dvirol; February 12th, 2009 at 06:19 AM.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: defining a template class problem

    This should clear up your syntax issues: http://www.parashift.com/c++-faq-lit...html#faq-35.16

    Here's how to use code tags for the next time you post code
    http://www.codeguru.com/forum/misc.php?do=bbcode#code

    gg

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: defining a template class problem

    Code:
    CGenPoly operator^ (int power);  // Omri
    This is not going to work as you expect. Operator ^ has low precedence, not high precedence as a true "power" operator would need.

    That's why you won't find any C++ based math libraries overloading operator ^ to do powers -- it just won't work correctly.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Feb 2009
    Posts
    5

    Re: defining a template class problem

    I am sorry for not posting my massage right.

    It was the first time for me.

    Thanks a lot for your answers.

  5. #5
    Join Date
    Feb 2009
    Posts
    5

    Re: defining a template class problem

    Paul,

    how low is the precedence of the Operator ^?

    is it even lower than +?

    what if I try it like that: "(x+1)^3"

    will it work?

  6. #6
    Join Date
    Feb 2009
    Posts
    5

    Re: defining a template class problem

    This should clear up your syntax issues: http://www.parashift.com/c++-faq-lit...html#faq-35.16

    Here's how to use code tags for the next time you post code
    http://www.codeguru.com/forum/misc.php?do=bbcode#code

    gg
    I tried using what was said there and it still won't work.

    I suspect the problem in my program is in this line
    Code:
    friend CGenPoly<T,var> operator* <T,var>(const T& elem, const CGenPoly& p);
    I also tried like that:
    Code:
    friend CGenPoly<T,var> operator* <T,var>(const <T>& elem, const CGenPoly& p);
    and it didn't work. I think I am using "T" wrong.
    (this line is found in the class declaration.)

    The following declaration for operator << is working:
    Code:
    friend ostream& operator << <T,var>(ostream& os, const CGenPoly& p);
    Can you see how I can fix it?

  7. #7
    Join Date
    Nov 2006
    Posts
    14

    Re: defining a template class problem

    Why is there a template parameter list in each of your friend function declarations?
    Code:
    friend CGenPoly operator * <T,var>(const T& elem, const CGenPoly& p);

  8. #8
    Join Date
    Jan 2009
    Posts
    596

    Re: defining a template class problem

    Quote Originally Posted by GrimmRiffer View Post
    Why is there a template parameter list in each of your friend function declarations?
    Code:
    friend CGenPoly operator * <T,var>(const T& elem, const CGenPoly& p);
    And this one doesn't even use them:
    Code:
    	friend ostream& operator << <T,var>(ostream& os, const CGenPoly& p); //Omri

  9. #9
    Join Date
    Feb 2009
    Posts
    5

    Re: defining a template class problem

    I got it working like this if you are interested:
    Code:
    public:
    	CGenPoly();
    	CGenPoly(T elem);
    	CGenPoly(const CGenPoly& p);
    	void SetCoef(int power,T elem);   //Omri
    	CGenPoly operator+ (const CGenPoly& p); 
    	CGenPoly operator* (const CGenPoly& p);
    	CGenPoly& operator+= (const CGenPoly& p);
    	CGenPoly& operator*= (const CGenPoly& p);    
    	CGenPoly& operator= (const CGenPoly& p);   //Omri
    	CGenPoly operator^ (int power);  // Omri
            CGenPoly operator* (const T& elem);
            CGenPoly operator+ (const T& elem);  //Omri
            T operator() (T point);    
            friend ostream& operator << <T,var>(ostream& os, const CGenPoly& p);  //Omr
    
    };
      template<class T,char var>
      CGenPoly<T,var> operator* (const T& elem,  CGenPoly<T,var>& p);
      template<class T,char var>
      CGenPoly<T,var> operator+ (const T& elem, CGenPoly<T,var>& p);  // Omri
    However, the code works only in VS.
    When I try to compile it with GNU g++ it still makes some problems.
    Please let me know if you can see why that happens, while I keep searching for it myself.
    Last edited by dvirol; February 12th, 2009 at 10:57 AM.

  10. #10
    Join Date
    Nov 2003
    Posts
    1,902

    Re: defining a template class problem

    >> Can you see how I can fix it?
    Go back and re-read the article. It shows all the various ways it can be fixed. You're trying to mix syntax rules for the various ways it could be fixed.

    Also, always assume that errors/warnings provided by the compiler are relevant when posting code that doesn't compile.

    Code:
    #include<iostream>
    using namespace std;
    
    // forward declarations of CGenPoly
    template<class T, char v> 
    class CGenPoly;
    
    // forward declaration to explicitly declare foo() as a template function
    template<class T, char v> 
    CGenPoly<T,v> foo(const CGenPoly<T,v> &p);
    
    template<class T, char v>
    class CGenPoly 
    {
    public:
        // template syntax not used, must be implemented inline
        friend ostream& operator << (ostream &os, const CGenPoly &p)
        {
            return os;
        }//operator ostream << CGenPoly
    
        // template parameters explicitly written on CGenPoly, must be inline
        friend CGenPoly<T,v> operator * (const T &elem, const CGenPoly<T,v> &p)
        {
            return CGenPoly<T,v>();
        }//operator*
    
        // template syntax used explicitly, (declaring new template parameter names
        // when implemented inline), function body may be moved outside the class
        // without a forward declaration since it's declared as a template function
        // here.
        template<class U, char v1>
        friend CGenPoly<U,v1> operator + (const U &elem, const CGenPoly<U,v1> &p)
        {
            return CGenPoly<U,v1>();
        }//operator+
    
        // "template" syntax not used, foo must be forward declared as a template 
        // and "<>" syntax used here so compiler knows we are referring to the 
        // template function foo (and not some other foo)
        // note: "<>" is more readable than "<T,v>" as a "I'm a template" declarator
        friend CGenPoly<T,v> foo<>(const CGenPoly<T,v> &p);
    };//CGenPoly
    
    template<class T, char v> 
    CGenPoly<T,v> foo(const CGenPoly<T,v> &p)
    {
        return CGenPoly<T,v>();
    }//foo
    
    int main()
    {
        CGenPoly<int, 'a'> test;
        cout << test << 1 + test << 1 * test << foo(test) << endl;
        return 0;
    }//main
    gg

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: defining a template class problem

    Quote Originally Posted by dvirol View Post
    Paul,

    how low is the precedence of the Operator ^?

    is it even lower than +?
    Yes.

    http://www.cppreference.com/wiki/operator_precedence
    what if I try it like that: "(x+1)^3"
    And what if I did this?
    Code:
    "x-1^3"
    Whatever you come up with, there is another that doesn't work. Basically, operator ^ cannot be used for powers, because it is easily broken with a simple mathematical expression. Operator ^ is a bitwise operator, not a mathematical operator. Using it for math operations is not what it's intended for.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured