473,401 Members | 2,139 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,401 software developers and data experts.

== operation on vectors

In the following program, I have used operator==() as member function
for learning purpose only.

consider the following program:

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test(int arg);
Test(const Test &rhs);
Test& operator=(const Test &rhs);
inline int value() const;
inline bool operator==(const Test &rhs) const;

private:
int val;
};

Test::Test(int arg) : val(arg)
{
}

Test::Test(const Test &rhs) : val(rhs.val)
{
}

Test &Test::operator=(const Test &rhs)
{
if (this != &rhs)
val = rhs.val;

return *this;
}

inline int Test::value() const
{
return val;
}

inline bool Test::operator==(const Test &rhs) const
{
return value() == rhs.value();
}

int main()
{
vector<Testc1;

c1.push_back(Test(10));

vector<Testc2;

c2.push_back(Test(10));

if (c1 == c2)
cout << "c1 equals c2" << endl;
else
cout << "not equal" << endl;

return EXIT_SUCCESS;
}

I compiled this program with g++ -std=c++98 -pedantic -Wall -Wextra.

It produced the output
c1 equals c2

However if I remove 'const' from the comparison member function
bool Test::operator==(const Test &rhs) const
ie if I have
bool Test::operator==(const Test &rhs)

Then I get compilation error for the line
if (c1 == c2)

What is the reason for the compilation error when Test::operator==()
is made non-const ?

If I remove operation==() from Test class and make it a free function,
then also the compilation error is gone and the program works fine as
it should be.

Kindly clarify.

Thanks
V.Subramanian
Aug 15 '08 #1
3 1594
su**************@yahoo.com, India wrote:
In the following program, I have used operator==() as member function
for learning purpose only.

consider the following program:

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test(int arg);
Test(const Test &rhs);
Test& operator=(const Test &rhs);
inline int value() const;
inline bool operator==(const Test &rhs) const;
'inline' means nothing if you don't supply the definition right after
the declaration. Besides, 'inline' is *implicit* if you *define* the
functions in the class definition. So you can safely remove the
'inline' from the two declarations above.
>
private:
int val;
};

Test::Test(int arg) : val(arg)
{
}

Test::Test(const Test &rhs) : val(rhs.val)
{
}

Test &Test::operator=(const Test &rhs)
{
if (this != &rhs)
val = rhs.val;

return *this;
}

inline int Test::value() const
{
return val;
}

inline bool Test::operator==(const Test &rhs) const
{
return value() == rhs.value();
}

int main()
{
vector<Testc1;

c1.push_back(Test(10));

vector<Testc2;

c2.push_back(Test(10));

if (c1 == c2)
cout << "c1 equals c2" << endl;
else
cout << "not equal" << endl;

return EXIT_SUCCESS;
}

I compiled this program with g++ -std=c++98 -pedantic -Wall -Wextra.

It produced the output
c1 equals c2

However if I remove 'const' from the comparison member function
bool Test::operator==(const Test &rhs) const
ie if I have
bool Test::operator==(const Test &rhs)

Then I get compilation error for the line
if (c1 == c2)
It might be useful if you actually posted what error you get...
>
What is the reason for the compilation error when Test::operator==()
is made non-const ?
It's quite possible that the operator== for vectors is defined to accept
two arguments, and both of them 'const'. The function then probably
attempts to do comparison of the sizes and then if they are the same,
the element-wise comparison of the storage. In a const vector the
storage is also most likely const. So, it looks for a function that
compares the stored types and both values are 'const'. If your
'Test::op==' needs a non-const lhs argument, the language cannot convert
the const reference it obtains from a const vector to a reference to
non-const object, so it complains.
>
If I remove operation==() from Test class and make it a free function,
then also the compilation error is gone and the program works fine as
it should be.
And define it how? Both arguments 'const'? Try removing const from one
of them or from both.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 15 '08 #2
su**************@yahoo.com, India wrote:
vector<Testc1;
c1.push_back(Test(10));
vector<Testc2;
c2.push_back(Test(10));

if (c1 == c2)
However if I remove 'const' from the comparison member function
bool Test::operator==(const Test &rhs) const
ie if I have
bool Test::operator==(const Test &rhs)

Then I get compilation error for the line
if (c1 == c2)

What is the reason for the compilation error when Test::operator==()
is made non-const ?
std::vector::operator==() is const, and thus cannot call non-const
functions for its elements.
If I remove operation==() from Test class and make it a free function,
then also the compilation error is gone and the program works fine as
it should be.
Because your free function takes both parameters as const references?
If you made it to take non-const references, compilation would probably
fail.
Aug 15 '08 #3
On Aug 15, 5:27 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
subramanian10...@yahoo.com, India wrote:
In the following program, I have used operator==() as member function
for learning purpose only.
consider the following program:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
class Test
{
public:
Test(int arg);
Test(const Test &rhs);
Test& operator=(const Test &rhs);
inline int value() const;
inline bool operator==(const Test &rhs) const;
'inline' means nothing if you don't supply the definition
right after the declaration. Besides, 'inline' is *implicit*
if you *define* the functions in the class definition. So you
can safely remove the 'inline' from the two declarations
above.
That's not right. It means exactly the same thing that it means
if you supply it later. There was once a compiler (an early
version of CFront, I think, but it was so long ago, I can't be
sure) that did require the inline on the first declaration of
the function. But the most widespread current practice is
certainly to only put it before the actual definition of the
function. (That way, you can change from inline to not inline
without touching the class definition.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 16 '08 #4

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

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
4
by: Tony | last post by:
Hello, For my application, I have a vector containing data that I need, vector<NODEnode_data; and I have a pointer of vectors which are sorted by some criterion as vector<NODE*np. I have planned...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
ashitpro
by: ashitpro | last post by:
Hello guys, I got two vectors, such as vector<string> A vector<string> B Both vectors are having more than 100 strings in it. I want to perform "minus" operation on it... i.e (A - B)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.