473,378 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

Jan 6 '06 #1
4 3194
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

Jan 6 '06 #2
Mi*************@tomtom.com 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

Jan 6 '06 #3
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

Jan 6 '06 #4
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

Jan 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
14
by: ambar.shome | last post by:
Hi, As you know there are few operators in C++ which cant be overloaded. They are: .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr , dynamic_cast , const_cast ,...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
2
by: subramanian100in | last post by:
overloaded operator=() -------------------------------- overloaded assignment operator should be a non-static MEMBER function of a class. This ensures that the first operand is an lvalue. If...
1
by: Nikhil.S.Ketkar | last post by:
Hi, Does the following construct qualify as overloading on return type ? If not, in what way ? I overloaded the type conversion operator and used pointers to member functions. Its pretty...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.