473,791 Members | 3,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

== 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==(cons t Test &rhs) const;

private:
int val;
};

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

Test::Test(cons t 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(Te st(10));

vector<Testc2;

c2.push_back(Te st(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 1611
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==(cons t 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(cons t 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(Te st(10));

vector<Testc2;

c2.push_back(Te st(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(Te st(10));
vector<Testc2;
c2.push_back(Te st(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::op erator==() 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...@com Acast.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==(cons t 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 objektorientier ter Datenverarbeitu ng
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
3418
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 objects in the vector vec_A. Suppose there are K attributes to be added. For each of the attributes I define K vectors of appropriate types. Say, the attributes have types type1, type2, ..., typeK. So I define std::vector<type1> attr1(vec_A.size());
5
2317
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 seem to be (in my eyes at least) are glorified Arrays. - Now I know there's a bit more difference, but what exactly are the advantages of Arrays over Vectors (if any)? Oh, and please keep it in mind I am a real beginner in C++ but can
3
3247
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 there anyway where I can accomodate both the vector types into a single set. Something like a set<vector<void*>, my_compare func >. Right now, I am having them as two different set dayatypes.
4
2024
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
18249
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 " << v1.size( ) << endl; v1.clear( ); //clears the vector I have a few questions:
2
8694
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 described in the text below. You do not need to change anything in main(). In void prob1(void), take a double floating-point number x from the keyboard and compute the function f(x), which is defined by:
4
1884
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 the data management as: Whenever I want to add a new node, I push_back onto the end of node_data and create a corresponding reference in np. However, I find that only the last-added pointer in np correctly points to the right
1
2067
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 function that handles vectors of vectors of <typename T(which could be anything from int to vectors of something else). As well, I have specialized/overloaded functions to handle the single-level vectors of data (e.g. vector<string>). So the templated...
2
2130
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) What should be the efficient method for this. I can change my data structures i.e. vector to Set or may be map....Data structure is not a problem..
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
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
10147
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
9987
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7531
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
6770
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
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2910
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.