why does my overloaded operator discard qualifiers



 DEVELOP > c-Plus-Plus > why does my overloaded operator discard qualifiers

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "gobis"
Date: 06 Jan 2006 05:45:14 AM
Object: why does my overloaded operator discard qualifiers
Hello everyone,
After I could not overload the << operator as a friend operator in my
template class (yes, it did not have to be a template class, but I was
testing something for the class I was going to teach) , I declared the
operator outside the class definition. This would prevent the operator
from accessing the private elements within the class, so I provided
access to elements of the private vector array thru a public member
function. But then I found that I could not place the const specifier
before the templated class argument in the overloaded operator
definition:
template <class C> ostream& operator << (ostream& os, const
Polynom<C>& p)
{
for(int i=p.GetOrder()+1; i>0; i--)
{
os << p.Coeff(i) << "*x^" << i << " + ";
}
os << p.Coeff(0) << endl;
return os;
}
The compiler (Bloodshed Dev C++) complained that passing a Polynom<int>
or a Polynom<double> instance to the operator would discard qualifiers.
I suppose the compiler wants the array member returned by the
Coeff(int) member function to be const as well, but I could not add the
const specifier in that definition. It did not supress the error
message.
Can anybody see what I am doing wrong?
Relevant code follows:
in the .hpp file:
template <class C> class Polynom
{
/*** Constructors and Destructors ***/
...
/*** Public methods ***/
...
C Coeff(int i);
...
/*** Data members ***/
private:
vector<C> coef;
};
in the .cpp file:
template <class C> C Polynom <C>::Coeff(int i)
{
return coef[i];
}
My regards,
Hurol Aslan
.

User: "Greg"

Title: Re: why does my overloaded operator discard qualifiers 06 Jan 2006 08:30:58 AM
gobis wrote:

Hello everyone,

After I could not overload the << operator as a friend operator in my
template class (yes, it did not have to be a template class, but I was
testing something for the class I was going to teach) , I declared the
operator outside the class definition. This would prevent the operator
from accessing the private elements within the class, so I provided
access to elements of the private vector array thru a public member
function. But then I found that I could not place the const specifier
before the templated class argument in the overloaded operator
definition:

template <class C> ostream& operator << (ostream& os, const
Polynom<C>& p)
{
for(int i=p.GetOrder()+1; i>0; i--)
{
os << p.Coeff(i) << "*x^" << i << " + ";
}
os << p.Coeff(0) << endl;
return os;
}

The compiler (Bloodshed Dev C++) complained that passing a Polynom<int>
or a Polynom<double> instance to the operator would discard qualifiers.
I suppose the compiler wants the array member returned by the
Coeff(int) member function to be const as well, but I could not add the
const specifier in that definition. It did not supress the error
message.

Can anybody see what I am doing wrong?
Relevant code follows:

in the .hpp file:

template <class C> class Polynom
{
/*** Constructors and Destructors ***/
...
/*** Public methods ***/
...
C Coeff(int i);

Redeclare the Coeff member function like so:
C Coeff(int i) const;
so that it can be called from within operator<<().

in the .cpp file:
template <class C> C Polynom <C>::Coeff(int i)
{
return coef[i];
}

Move this definition of Coeff() into the Polynom's header file, so that
it is visible to client code. Class template implementations generally
are implemented completely in a .h file and have no .cpp file at all.
Greg
.

User: "Greg"

Title: Re: why does my overloaded operator discard qualifiers 06 Jan 2006 08:27:30 AM
gobis wrote:

Hello everyone,

After I could not overload the << operator as a friend operator in my
template class (yes, it did not have to be a template class, but I was
testing something for the class I was going to teach) , I declared the
operator outside the class definition. This would prevent the operator
from accessing the private elements within the class, so I provided
access to elements of the private vector array thru a public member
function. But then I found that I could not place the const specifier
before the templated class argument in the overloaded operator
definition:

template <class C> ostream& operator << (ostream& os, const
Polynom<C>& p)
{
for(int i=p.GetOrder()+1; i>0; i--)
{
os << p.Coeff(i) << "*x^" << i << " + ";
}
os << p.Coeff(0) << endl;
return os;
}

The compiler (Bloodshed Dev C++) complained that passing a Polynom<int>
or a Polynom<double> instance to the operator would discard qualifiers.
I suppose the compiler wants the array member returned by the
Coeff(int) member function to be const as well, but I could not add the
const specifier in that definition. It did not supress the error
message.

Can anybody see what I am doing wrong?
Relevant code follows:

in the .hpp file:

template <class C> class Polynom
{
/*** Constructors and Destructors ***/
...
/*** Public methods ***/
...
C Coeff(int i);

Redeclare the above member function as:
C Coeff(int i) const;
so that it can be called operator<<().

...
/*** Data members ***/
private:
vector<C> coef;
};

in the .cpp file:

template <class C> C Polynom <C>::Coeff(int i)
{
return coef[i];
}

Move this definition of Coeff() into the Polynom's header file, so that
it is visible to client code. Class template implementations generally
are implemented completely in a .h file and have no .cpp file at all.
Greg
.

User: ""

Title: Re: why does my overloaded operator discard qualifiers 06 Jan 2006 05:53:23 AM
gobis wrote:

template <class C> ostream& operator << (ostream& os, const
Polynom<C>& p)
{
for(int i=p.GetOrder()+1; i>0; i--)
{
os << p.Coeff(i) << "*x^" << i << " + ";
}
os << p.Coeff(0) << endl;
return os;
}
in the .hpp file:

template <class C> class Polynom
{
/*** Constructors and Destructors ***/
...
/*** Public methods ***/
...
C Coeff(int i);
...
/*** Data members ***/
private:
vector<C> coef;
};

in the .cpp file:

template <class C> C Polynom <C>::Coeff(int i)
{
return coef[i];
}

Two issues: Coeff can't change this->coef, so it should be const
(so it can be called on a Polynom const&) and your compiler doesn't
have the keyword 'export' which you need to put a template definition
in a .cpp file.
HTH,
Michiel Salters
.
User: "Luke Meyers"

Title: Re: why does my overloaded operator discard qualifiers 06 Jan 2006 06:17:38 AM
wrote:

your compiler doesn't
have the keyword 'export' which you need to put a template definition
in a .cpp file.

That's a bit disingenuous. See the FAQ for how to handle this. The
approach is not to put everything in the header file, but rather to
#include the .cpp (just once) at some point to avoid linker errors.
It's actually pretty nice in terms of compile times, under some
configurations.
Luke
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER