Please pardon my ignorance. I'm writing a C++ STL program after not writing code for 7 years, I'm trying to invoke key_word.inc() using an iterator for a set containing key_word_T. The following is a pared down version. - #include <cstdlib>
-
#include <set>
-
#include <string>
-
using namespace std;
-
-
class key_word
-
{
-
public:
-
string word (void) const { return (wordX); }
-
void inc (void) { ++countX; }
-
private:
-
string wordX;
-
int countX;
-
}; /* class key_word */
-
-
typedef class key_word key_word_T;
-
-
class key_word_sort
-
{
-
public:
-
bool operator () (const key_word_T& leftA, const key_word_T& rightA) const
-
{
-
return (leftA.word() < rightA.word());
-
} /* operator () */
-
}; /* class key_word_sort */
-
-
typedef class key_word_sort key_word_sort_T;
-
-
set <key_word_T, key_word_sort_T> global_keys; // all keywords
-
set <key_word_T, key_word_sort_T> local_keys; // this document's keywords
-
pair<set<key_word_T, key_word_sort_T>::iterator, bool> global_result;
-
pair<set<key_word_T, key_word_sort_T>::iterator, bool> local_result;
-
set<key_word_T, key_word_sort_T>::iterator global_itor;
-
set<key_word_T, key_word_sort_T>::iterator local_itor;
-
-
string one_word;
-
-
int main (int, char**)
-
{
-
one_word = "$FOOBAR";
-
-
global_result = global_keys.insert (one_word); // compiles in full code, not in example
-
key_word_T local_key(*global_result.first);
-
local_result = local_keys.insert (local_key);
-
global_itor = global_result.first;
-
(*global_itor).inc(); // want to invoke key_word.inc() on global_keys copy
-
-
-
return (EXIT_SUCCESS);
-
} /* main () */
It looks like dereferenced iterator is a const reference, so you can call only 'const' members with it as 'this'. I could not find proof of it in online references, but it is easily explained - if you could modify set members in some other way then insertion/deletion, it may violate their order, make them non-unique, etc.
4 1878 Banfa 9,065
Expert Mod 8TB @jfwfmt
Your typedefs are unnecessary in C++ you can use the class name as the type name without the class keyword (same goes for structs unions and enums) so you can declare your variables as -
set <key_word, key_word_sort> global_keys; // all keywords
-
set <key_word, key_word_sort> local_keys; // this document's keywords
-
pair<set<key_word, key_word_sort>::iterator, bool> global_result;
-
pair<set<key_word, key_word_sort>::iterator, bool> local_result;
-
set<key_word, key_word_sort>::iterator global_itor;
-
set<key_word, key_word_sort>::iterator local_itor;
-
Talking of variables that is an awful lot of global variables you have there, I would consider that bad practice in C let alone C++.
(*global_itor).inc(); // want to invoke key_word.inc() on global_keys copy
The iterator is a pointer to your type so this should work although I would write it as
global_itor->inc(); // want to invoke key_word.inc() on global_keys copy
but the 2 syntaxes have the same meaning.
Did it produce any compiler diagnostics?
Thanks for the style suggestions
and
produce the identical error message (g++)
error:passing 'const key_word' as 'this' argument of 'void key_word::inc()' discards qualifiers
It looks like dereferenced iterator is a const reference, so you can call only 'const' members with it as 'this'. I could not find proof of it in online references, but it is easily explained - if you could modify set members in some other way then insertion/deletion, it may violate their order, make them non-unique, etc.
Thanks, your hint about const ness was the clue. It turns out that STL implementations differ on the const of set objects. GNU makes them const.
Meyers shows a workaround for non-key data members of the objects in the set, using a cast to alter the const ness of the reference - const_cast<key_word_T&>(*global_itor).inc();
is the fix
/s/ Jim WIlliams
Sign in to post your reply or Sign up for a free account.
Similar topics
by: jimjim |
last post by:
Hello,
My question concerns as to how a pointer is passed by reference as a
function argument. The following is from code taken from the MICO
implementation of the CORBA specification.
in...
|
by: wASP |
last post by:
Hi,
I'm having a problem referencing the elements within an object
after a method of that object (a member function) has been activated
with an onsubmit handler:
- - - - - - - - ...
|
by: Richard Lewis Haggard |
last post by:
Is it possible to put a reference to an object inside of a class? If so,
what is the syntax? The reason I want to do this is so that I can make a
copy of the original object, make modifications to...
|
by: Dilip |
last post by:
I have a vector of class object pointers that have 2 internal states
(USED or EMPTY). USED is just a way of indicating the element in that
slot has valid state. EMPTY means the element is...
|
by: toton |
last post by:
HI,
One more small doubt from today's mail.
I have certain function which returns a pointer (sometimes a const
pointer from a const member function).
And certain member function needs reference...
|
by: SunnyDrake |
last post by:
HI!
I wrting some program part of it is XML config parser which contains
some commands(for flexibility of engenie).
how do i more simple(if it possible not via System.Reflection or...
|
by: shuisheng |
last post by:
Dear All,
The problem of choosing pointer or reference is always confusing me.
Would you please give me some suggestion on it. I appreciate your kind
help.
For example, I'd like to convert a...
|
by: Howard |
last post by:
Hi,
I've got a program whose main object contains an array of "cells", each
of which contains an array of "sub-cells", each of which contains an array
of "sub-sub-cells". The cells, subcells...
|
by: Turin |
last post by:
Dear all;
As far as I understand the idea behind getter methods, it is used to
make sure that private memers of a class is returned appropriately to
the calling object.
However, if all I am...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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: 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,...
|
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,...
|
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...
|
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...
|
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,...
| |