473,804 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t i)
{
return coef[i];
}

My regards,
Hurol Aslan

Jan 6 '06 #1
4 3218
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(in t 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(in t 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(in t 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
8919
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 expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
2
1691
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 as you can see in the constructor. Class Matris should create a matris by using class Intvektor. So what I want to have is a pointer to an array of Intvektor and in each positionindex in this array will I have a pointer to an array of Intvektor
14
3663
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 , reinterpret_cast . Theremust be some reason for this restriction for each of the
1
10012
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 A, and the second one of type B. Let say, these are not my classes and I know nothing about the implementation. As system doesn't know what code must be used for resolving the language construction a+b (where A a; and B b;), it returns "The call...
17
2338
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 << endl;
2
7379
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 number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
9
8182
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); mmFree(&local_Head,(char *)mem); CPRMemory::SetMemoryHeadAs(local_Head,head_type); } ///////////////////// void* operator new(size_t sz, int head_Type) {
2
2118
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 the overloaded assignment operator function is allowed to be a non- member function then we may be able to write the following: Suppose we have a class Test for which operator+() is defined, suppose we have
1
1507
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 straightforward but I am sure I have missed as this is not supposed to be possible. Am I misunderstanding overloading ?
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.