-
January 26th, 2012, 09:33 AM
#1
std::make_pair
Hello,
I'm using std::make_pair, to fill a std:air. I only get some error and I can't find out why.
Code:
std::pair <LONG,LONG> ControlSize;
ControlSize = std::make_pair(rc.right - rc.left,rc.bottom - rc.top);
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std:air<_Ty1,_Ty2>' (or there is no acceptable conversion)
Grz
-
January 26th, 2012, 10:18 AM
#2
Re: std::make_pair
this compiles
Code:
#include <utility>
#include <Windows.h>
int main()
{
RECT rc = {0};
std::pair <LONG,LONG> ControlSize;
ControlSize = std::make_pair(rc.right - rc.left,rc.bottom - rc.top);
return 0;
}
this does not
Code:
#include <utility>
#include <Windows.h>
int main()
{
RECT rc = {0};
const std::pair <LONG,LONG> ControlSize;
ControlSize = std::make_pair(rc.right - rc.left,rc.bottom - rc.top);
return 0;
}
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::pair<_Ty1,_Ty2>' (or there is no acceptable conversion)
Notice the const. Once the ControlSize has been initialized, there is no changing it without breaking const, but you can initialize it with make_pair like
Code:
#include <utility>
#include <Windows.h>
int main()
{
RECT rc = {0};
const std::pair <LONG,LONG> ControlSize = std::make_pair(rc.right - rc.left,rc.bottom - rc.top);
return 0;
}
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
January 26th, 2012, 10:34 AM
#3
Re: std::make_pair
I never use const infront of it. I use it as the first example and it doesn NOT work. That's what is so weird, or there is a reason I don't know yet.
-
January 26th, 2012, 10:49 AM
#4
Re: std::make_pair
Originally Posted by NLscotty
I never use const infront of it. I use it as the first example and it doesn NOT work. That's what is so weird, or there is a reason I don't know yet.
If you get a compiler error, you should post a minimal, but complete code example. Your example is incomplete -- it's just two lines copied from some function.
You should also post the compiler your using, maker and version.
Here is a complete example that compiles successfully:
Code:
#include <map>
int main()
{
std::pair <long, long> ControlSize;
ControlSize = std::make_pair(0, 0);
}
This was compiled using the online Comeau C++ compiler. There are no errors in this code.
Regards,
Paul McKenzie
-
January 26th, 2012, 01:30 PM
#5
Re: std::make_pair
Oki, ill post some more code
Code:
class WinTraits
{
public:
WinTraits();
//,,,,,,,,,,,,,,,
//,,,,,,,,,,,,,,,
private:
std::pair <LONG,LONG> ControlSize;
//...............
//................
};
WinTraits::WinTraits()
: //.................
ControlSize(0,0)
{
//..........
}
std::pair<LONG,LONG> WinTraits::GetSize() const
{
if(!hwnd)
return ControlSize;
RECT rc = {0};
::GetWindowRect(hwnd, &rc);
ControlSize = std::make_pair<LONG,LONG>(rc.right - rc.left,rc.bottom - rc.top);
return ControlSize
}
That's all the code with 'ControlSize'. Hope this helps.
-
January 26th, 2012, 01:32 PM
#6
Re: std::make_pair
Code:
std::pair<LONG,LONG> WinTraits::GetSize() const
you have choosen not to modify any class member by placing that const in red, but you have just modified one.
-
January 26th, 2012, 02:25 PM
#7
Re: std::make_pair
Originally Posted by SkyNetTo
Code:
std::pair<LONG,LONG> WinTraits::GetSize() const
you have choosen not to modify any class member by placing that const in red, but you have just modified one.
Omg, so stupid. Thanks a lot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|