why does my overloaded operator discard qualifiers

G

gobis

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;
}

My regards,
Hurol Aslan
 
M

Michiel.Salters

gobis said:
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;
}


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
 
L

Luke Meyers

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
 
G

Greg

gobis said:
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;
}


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
 
G

Greg

gobis said:
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;
}


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top