-
February 11th, 2009, 05:10 PM
#1
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.
-
February 11th, 2009, 08:33 PM
#2
Re: defining a template class problem
-
February 11th, 2009, 08:55 PM
#3
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
-
February 12th, 2009, 05:04 AM
#4
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.
-
February 12th, 2009, 06:18 AM
#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?
-
February 12th, 2009, 06:56 AM
#6
Re: defining a template class problem
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?
-
February 12th, 2009, 09:28 AM
#7
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);
-
February 12th, 2009, 09:35 AM
#8
Re: defining a template class problem
Originally Posted by GrimmRiffer
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
-
February 12th, 2009, 10:55 AM
#9
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.
-
February 12th, 2009, 10:57 AM
#10
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
-
February 12th, 2009, 01:23 PM
#11
Re: defining a template class problem
Originally Posted by dvirol
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?
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|