3

I have created a list of strings like this:

typedef CList<CString, CString&> MyListOfStrings;
MyListOfStrings aListOfStrings;

It compiles fine. Now I want to replace CString with CStringArray like this:

typedef CList<CStringArray, CStringArray&> MyListOfStringArrays;
MyListOfStringArrays aListOfStringArrays;

However, I get this mysterious error C2280:

error C2280: 'CStringArray &CStringArray::operator =(const CStringArray &)': attempting to reference a deleted function

Does anyone have a clue?

11
  • I have read this: link but it doesn't make any sense to me. Commented Sep 22 at 22:48
  • 1
    Since CStringArray derives from CObject, it's not copyable. Apparently CList requires its element type to be copyable. The function in the error message is the copy assignment operator. Commented Sep 22 at 22:53
  • 6
    You'd probably be happier with std::vector<CString> and std::vector<std::vector<CString>>. MFC collection classes are ancient, and rather poorly designed, at least by modern standards. Commented Sep 22 at 22:56
  • 2
    Usage note: Rather than adding information in a comment, edit the question and add the missing information. This keeps all of the question information in one place. Commented Sep 22 at 23:52
  • 2
    @KennethSutherland Other than probably CString, the MFC container classes are obsolete. Use one of the standard C++ container classes. Commented Sep 23 at 0:51

1 Answer 1

6

The error message you get means that CStringArray does not have an assignment operator (operator=).

CList seems to be attempting to use such an assignment (possibly to copy elements around) and therefore you get a compilation error.

As suggested in comments, a possible solution is to use standard C++ containers instead. This is recommended anyway since MFC's container classes are quite ancient and not necessarily up-to-date with modern C++ features.

Since CList lists behave like doubly-linked lists, you can consider to use std::list for it. And for a "sequential" (array-like) container it is recommeneded to use std::vector (or std::array for fixed sized ones).

#include <list>
#include <vector>

//...

std::list<std::vector<CString>> aListOfStringArrays;

Note that a linked-list is less cache-friendly that an array-like sequential containers and therefore usually less efficient. So you can even consider to use std::vector instead of the outer std::list:

std::vector<std::vector<CString>> anArrayOfStringArrays;

A side note:
If you want to introduce types for the list/array, instead of typedef you can use the more modern C++ type alias, e.g.:

using CListOfStringArrays = std::list<std::vector<CString>>;
Sign up to request clarification or add additional context in comments.

Comments

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.