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

How to eliminate assignment operator generation warning

I'm using Microsoft Development Environment 2002 Version 7.0.9466 on
Windows XP Professional Service Pack 2. If I compile the following
code at maximum warning level:

#include <functional>

class SetValue : public std::unary_function<int, void>
{
public:
explicit SetValue(int& value) : m_value(value) {}
void operator() (int value) { m_value = value; }
private:
int& m_value;
};
I get the following warning:
.... warning C4512: 'SetValue' : assignment operator could not be
generated
... see declaration of 'SetValue'

I can't see anything in the declaration of this class that would
prevent the compiler from generating an assignment operator.

What is causing the warning and how can I eliminate it?

Nov 17 '05 #1
8 1501
>I'm using Microsoft Development Environment 2002 Version 7.0.9466 on
Windows XP Professional Service Pack 2. If I compile the following
code at maximum warning level:
...
I get the following warning:
... warning C4512: 'SetValue' : assignment operator could not be
generated
... see declaration of 'SetValue'
FWIW, the Comeau online compiler agrees with VC++ - it reports a more
useful message:

"ComeauTest.c", line 3: error: implicitly generated assignment
operator cannot copy:
reference member "SetValue::m_value"
What is causing the warning and how can I eliminate it?


Add an assignment operator to the class.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
Hi tron!
class SetValue : public std::unary_function<int, void>
{
public:
explicit SetValue(int& value) : m_value(value) {}
void operator() (int value) { m_value = value; }
private:
int& m_value;
};


Is this a good idea to store a pointer to a value?

I would only do: int m_value;

Or what is the reason for this?

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #3
What is the reason the reference variable m_value cannot be copied? As
far as I know there should be no problem with this. It is perfectly
legal to do the following:

int value = 42;
int& first = value
int& second = first; // copy of the first reference
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #4
Tron Thomas wrote:
What is the reason the reference variable m_value cannot be copied?
As far as I know there should be no problem with this. It is
perfectly legal to do the following:
References cannot be copied. Ever.

int value = 42;
int& first = value
int& second = first; // copy of the first reference


Not really a copy of the first reference, but a new reference to 'value'.

Here's the case that matters though:

int value;
int& first = value;
int other;
int& second = other;

// legal, but doesn't assign the reference
// instead, 'other' is set to the same value as 'value'.
second = first;

-cd
Nov 17 '05 #5
The example I posted was meant to be a simpler example of the real
functor I'm using that is creating the warning. The following is more
or less what I'm trying to do, only I'm not using integers:

class UpdateBounds : public std::unary_function<int, void>
{
public:
UpdateBounds(int* pMaxValue, int* pMinValue) :
m_maxValue(*pMaxValue), m_minValue(*pMinValue) {}
void operator ()(int value)
{
if(value > m_maxValue){
m_maxValue = value;
}
else if(value < m_minValue){
m_minValue = value;
}
}
private:
int& m_maxValue;
int& m_minValue;
};

typedef std::vector<int> IntVector;
typedef IntVector::iterator IntIterator;

IntVector values;
// Populate the values ...

IntIterator itBegin = values.begin();
int maxValue = *itBegin;
int minValue = maxValue;

std::for_each(++itBegin, values.end(), UpdateBounds(&maxValue,
&minValue));
After this code has completed, the maxValue and minValue variables will
contain the maximum and minimum values (i.e. the bounds)respectively of
the values vector with only one pass through that vector.

If there is a better way to accomplish this, please let me know. Also,
please excuse any errors in the above code. I did not compile it to
make sure it was correct.
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #6
Right, in my original test, I confused assignment with initialization.

With the original code I posted someone could do the following:

int value = 42;
SetValue functor(value);
SetValue anotherFunctor = functor; // this is initialization, not
assignment.

Someone cannot do this:
int anotherValue = 13;
SetValue anotherFunctor(anotherValue);
anotherFunctor = functor; // this is assignment and will fail because
the reference can't be assigned to refer to something else

I don't need or want the functor to be assigned to another functor, so I
will just declare a private assignment operator and not provide any
implementation.
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #7
Tron Thomas wrote:
Right, in my original test, I confused assignment with initialization.

With the original code I posted someone could do the following:

int value = 42;
SetValue functor(value);
SetValue anotherFunctor = functor; // this is initialization, not
assignment.

Someone cannot do this:
int anotherValue = 13;
SetValue anotherFunctor(anotherValue);
anotherFunctor = functor; // this is assignment and will fail because
the reference can't be assigned to refer to something else

I don't need or want the functor to be assigned to another functor,
so I will just declare a private assignment operator and not provide
any implementation.


That's the appropriate solution to eliminate the warning.

-cd
Nov 17 '05 #8
Tron Thomas wrote:

[snip code for algorithm to calculate min and max element of a sequence
using for_each]
If there is a better way to accomplish this, please let me know. Also,
please excuse any errors in the above code. I did not compile it to
make sure it was correct.


I think a better approach would be to write a min_max_element algorithm,
that would return a std::pair<It, It>. e.g.

template <class FwdIt>
std::pair<FwdIt, FwdIt> min_max_element(FwdIt begin, FwdIt end)
{
std::pair<FwdIt, FwdIt> min_max(begin, begin);
for (; begin != end; ++begin)
{
if (*begin < *min_max.first)
min_max.first = begin;
else if (*min_max.second < *begin)
min_max.second = begin;
}
return min_max;
}

You could also add an overload taking a predicate. Finally, if you
prefer, you could just use std::min_element and std::max_element, but
that would be a two pass approach.

As a general rule, don't use for_each if a more appropriate algorithm
exists (or you can write one).

Tom
Nov 17 '05 #9

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

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
2
by: blackswift | last post by:
hello ,all i got this warning: under emacs using GCC 4.0.1 gpp(DJGPP) compiler: gpp -Wall -g lizards.cpp Invalid keyboard code specified lizards.cpp: In function 'int maxflow(int (*), int,...
2
by: Gerard Kramer | last post by:
Hello, There is a slight problem with operator overloading in a program I attempt to start practising C++. It is a basic (not very original) game of life simulator. It uses two classes:...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
12
by: Alan Ning | last post by:
I have a question on assignment operator. People in my company likes to make their assignment operator const. So we have const A& operator=(const A& obSrc) But everywhere online, I see
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
24
by: boblatest | last post by:
Hello, I have an if-elif chain in which I'd like to match a string against several regular expressions. Also I'd like to use the match groups within the respective elif... block. The C-like...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
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: 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?
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...
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
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,...
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...

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.