oro777
oro777

Reputation: 1110

MFC structure array

I would like to create an array of structure in C++. I do not know the size so I would like to make it dynamic. I have tried with CList but I have failed. So I tried using a vector but I have the same error.

I have the following structure in my header :

typedef CArray<CArray<double, double>,double> ObjectArray;
struct MyStruct {
  CString type;
  CString name;
  ObjectArray value;
};
std::vector<MyStruct> ST;

In my cpp file, I have the following code to populate my array. STR is a structure I populated beforehand.

ST.push_back(STR);

I have the following error when building:

c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxtempl.h(262): error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject

I guess I am doing something wrong but I do not understand what I am missing.

Upvotes: 0

Views: 1811

Answers (2)

PaulMcKenzie
PaulMcKenzie

Reputation: 35454

Instead of mixing MFC containers with STL containers, why not use std::vector throughout?

#include <vector>
typedef std::vector<std::vector<double> > ObjectArray;
struct MyStruct {
    CString type;
    CString name;
    ObjectArray value;
};

std::vector<MyStruct> ST;

The CString is OK (CString has its uses, and is also copyable). However in this day and age, using the MFC containers instead of STL containers is rarely, if ever necessary. I have read that even Microsoft doesn't use them anymore in their C++ based programs and now only use the STL containers.

Upvotes: 2

JohnB
JohnB

Reputation: 13723

If you derive a class from CObject, you have to provide a custom copy constructor, see here:

http://msdn.microsoft.com/en-us/library/st9cdfkz.aspx

"The standard C++ default class copy constructor does a member-by-member copy. The presence of the private CObject copy constructor guarantees a compiler error message if the copy constructor of your class is needed but not available. You must therefore provide a copy constructor if your class requires this capability."

I guess CArray inside the struct generates the problem, as it is derived from CObject and does not define a custom copy constructor, see here:

http://msdn.microsoft.com/en-us/library/4h2f09ct.aspx

Either don't use it, or derive some CDerivedArray from CArray and define a custom copy constructor there.

Why don't you use std::vector or std::map or another STL class rather than CArray?

Upvotes: 1

Related Questions