CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: std::make_pair

  1. #1
    Join Date
    Aug 2009
    Posts
    219

    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

  2. #2
    Join Date
    Jun 2008
    Posts
    592

    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

  3. #3
    Join Date
    Aug 2009
    Posts
    219

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: std::make_pair

    Quote Originally Posted by NLscotty View Post
    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

  5. #5
    Join Date
    Aug 2009
    Posts
    219

    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.

  6. #6
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    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.

  7. #7
    Join Date
    Aug 2009
    Posts
    219

    Re: std::make_pair

    Quote Originally Posted by SkyNetTo View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured