473,387 Members | 1,516 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,387 software developers and data experts.

Assignement operator, templates, double associative array

I'm building DataManager<T>. A class where shared data will be stored.
One of things useful to implement is garbage collection. But it still
gives me this error:

stl_algo.h:1076: error: non-static const member `const std::string
DataMapPair::first', can't use default assignment operator

Here's my code:

typedef std::map< std::string, std::pair< bool, CountedPtr<T >
DataMap;
typedef std::pair< std::string, std::pair< bool, CountedPtr<T >
DataMapPair;

DataMap dataMap_;

template <typename Tbool DataManager<T::
IsGarbage( const DataMapPair& dmPair )
{
return ( dmPair.second.first && dmPair.second.second.IsUnique() );
}

template <typename Tvoid DataManager<T::
CollectGarbage()
{
dataMap_.erase( remove_if( dataMap_.begin(), dataMap_.end(),
IsGarbage ), dataMap_.end() );
}

Can anyone tell how should I oveload that operator=? Or should I do
something else?

Feb 20 '07 #1
4 1774
ms****@gmail.com wrote:
I'm building DataManager<T>. A class where shared data will be stored.
One of things useful to implement is garbage collection. But it still
gives me this error:

stl_algo.h:1076: error: non-static const member `const std::string
DataMapPair::first', can't use default assignment operator

Here's my code:

typedef std::map< std::string, std::pair< bool, CountedPtr<T >
DataMap;
typedef std::pair< std::string, std::pair< bool, CountedPtr<T >
DataMapPair;

DataMap dataMap_;

template <typename Tbool DataManager<T::
IsGarbage( const DataMapPair& dmPair )
{
return ( dmPair.second.first && dmPair.second.second.IsUnique() );
}

template <typename Tvoid DataManager<T::
CollectGarbage()
{
dataMap_.erase( remove_if( dataMap_.begin(), dataMap_.end(),
IsGarbage ), dataMap_.end() );
}

Can anyone tell how should I oveload that operator=? Or should I do
something else?
remove_if tries to rearrange the sequence it is given. std::map's are
stored in a fixed order (i.e. they are ordered by a key) and they don't
like it when you try to rearrange them. That is what the error message
means.

Instead of trying fancy stuff in your CollectGarbage method you should
just write a loop through the entries of the map and delete any that are
garbage.

john

Feb 20 '07 #2
On Feb 20, 6:56 pm, John Harrison <john_androni...@hotmail.comwrote:
msc...@gmail.com wrote:
I'm building DataManager<T>. A class where shared data will be stored.
One of things useful to implement is garbage collection. But it still
gives me this error:
stl_algo.h:1076: error: non-static const member `const std::string
DataMapPair::first', can't use default assignment operator
Here's my code:
typedef std::map< std::string, std::pair< bool, CountedPtr<T >
DataMap;
typedef std::pair< std::string, std::pair< bool, CountedPtr<T >
DataMapPair;
DataMap dataMap_;
template <typename Tbool DataManager<T::
IsGarbage( const DataMapPair& dmPair )
{
return ( dmPair.second.first && dmPair.second.second.IsUnique() );
}
template <typename Tvoid DataManager<T::
CollectGarbage()
{
dataMap_.erase( remove_if( dataMap_.begin(), dataMap_.end(),
IsGarbage ), dataMap_.end() );
}
Can anyone tell how should I oveload that operator=? Or should I do
something else?

remove_if tries to rearrange the sequence it is given. std::map's are
stored in a fixed order (i.e. they are ordered by a key) and they don't
like it when you try to rearrange them. That is what the error message
means.

Instead of trying fancy stuff in your CollectGarbage method you should
just write a loop through the entries of the map and delete any that are
garbage.

john
Thank you very much... I should have known.
Feb 20 '07 #3
ms****@gmail.com wrote in news:1171994616.455888.110910
@a75g2000cwd.googlegroups.com:
>>
typedef std::map< std::string, std::pair< bool, CountedPtr<T >
DataMap;
typedef std::pair< std::string, std::pair< bool, CountedPtr<T >
DataMapPair;

Out of curiosity, any reason why the second line isn't:

typedef DataMap::value_type DataMapPair;

? Or simply use DataMap::value_type directly?
Feb 20 '07 #4
Out of curiosity, any reason why the second line isn't:
>
typedef DataMap::value_type DataMapPair;

? Or simply use DataMap::value_type directly?
I'm still not so IN the STL as I should be... And I didn't know
construction like this exists. Currently reading a book about STL form
Josuttis, so hopefully I'll get better.
Feb 20 '07 #5

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

Similar topics

5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Michael DeWulf | last post by:
I am trying to make a 2D matrix class. The data in the matrix will be of type int and so the underlying data structure will be a 2D array (int ** matrix). To make the data easy to modify, I would...
17
by: Alexander Eisenhuth | last post by:
Hello, is there a assignement operator, that i can overwrite? class MyInt: def __init__(self, val): assert(isinstance(val, int)) self._val = val a = MyInt(10)
11
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.