Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a compilation error when using a MFC CList with CStringArray elements

Tags:

c++

mfc

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?

like image 505
Kenneth Sutherland Avatar asked Dec 22 '25 13:12

Kenneth Sutherland


1 Answers

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>>;
like image 169
wohlstad Avatar answered Dec 24 '25 01:12

wohlstad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!