3

MFC beginner here.

I've tried to initialize std::map like this: (in the header of CView)

// myprogramView.h
std::map<int, CStatic> myMap = {{10,{}}, {11,{}}};

But the compiler complains "no instance of constructor ... matches the argument list".

(Edit for future reference) The above message was an error from IntelliSense. The compiler(MSVC) says: C2664 'std::map<int,CStatic,std::less,std::allocator<std::pair<const _Kty,_Ty>>>::map(std::initializer_list<std::pair<const _Kty,_Ty>>)': cannot convert argument 1 from 'initializer list' to 'std::initializer_list<std::pair<const _Kty,_Ty>>'

However, we can do these kinds of initializations:

std::map<int, std::string> myMap2 = { {10,{}}, {11,{}} };
std::map<int, std::map<std::string, int>> myMap3 = { {10,{}}, {11,{}} };

Why doesn't the first example compile, and how can I use a map containing MFC objects?

I'm trying to access the control object in the map and .Create() it during the run-time or in the OnCreate.

I also tried CMap but it seems the same problem occurs.


Question resolved by j6t's answer. Just want to clarify that the answer is talking about std::initializer_list, not 'member initializer list'.

6
  • 4
    Please post the entire error message. Commented Nov 3, 2022 at 11:45
  • Seems unusual to want to initialize a std::map with values that aren't ultimately going to be used. What's the real problem you are trying to solve here? Commented Nov 3, 2022 at 11:46
  • @IInspectable I just wanted to make some labels for texts, in a for loop. So I thought creating a map containing CStatics would be convenient, since I can access them by key. (the key will be different in the for loop). Maybe I could use std::vector, etc., this was just a quick idea. Commented Nov 3, 2022 at 12:09
  • 1
    Why don't you just store a map of control IDs and their respective text entries? Or really, if this is all known at compile time, why not simply an array of structs that contain the ID and text? CWnd::GetDlgItem turns a control ID into a control. Or simply call CWnd::SetDlgItemText. Commented Nov 3, 2022 at 12:14
  • 1
    The window classes in an SDI project derive from CWnd, so you can just use those members as is. Commented Nov 3, 2022 at 12:31

1 Answer 1

6

MFC objects that derive from CObject (like CStatic) cannot be copied; they have a deleted copy constructor. But initialization from an initializer list requires objects that are copy-constructible.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks! Is there any reason MFC objects are designed to be non-copyable?
Yes: Serialization support.
Yes. GUI elements have "identity" and must not be copied.
No, that's not the reason. Clearly, a CArray isn't a GUI element (but derives from CObject, so it isn't copyable either).
@starriet Serialization support requires that you derive from CObject. CObject has a private copy-c'tor to prevent accidental pass-by-value. Obviously, one can devise a serialization strategy for any data type. But if you want to use the built-in serialization of MFC, you'll have to derive from CObject and inherit its property of not being copyable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.