本文介绍了使用CAtlArray作为类成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果有人可以为我提供一个使用CAtlArray作为类中的成员变量的代码示例,我将不胜感激,在该类中数组应保存自定义数据结构.
我已经定义了数据结构:
I would appreciate it if someone can provide me with a code sample for using CAtlArray as a member variable inside a class, where the array should hold a custom data structure.
I have defined the data structure:
typedef struct
{
int Status;
CTime StatusDate;
} MyDataStructure;
然后,在我的课堂上,我定义:
Then, in my class, I define:
private:
CAtlArray<MyDataStructure> MyArray;
当我调用RemoveAll()或尝试添加新项目时,出现访问冲突错误.
在添加成员之前,我尝试使用SetCount(),但它也无法正常工作.
When I call RemoveAll(), or try to add a new item, I get an Access Violation error.
I tried to use SetCount() prior to adding members, but it didn''t work as well.
推荐答案
MyDataStructure* pNewStruct;
pNewStruct =(MyDataStructure* )malloc(sizeof(MyDataStructure));
MyArray.Add(pNewStruct);
希望这会起作用.
Hope this will work.
#include "stdafx.h"
#include <atlbase.h>
#include <atltime.h>
#include <atlcoll.h>
#include <atldebugapi.h>
class MyClass
{
public:
typedef struct
{
CString* FullPath;
CTime* StatusDate;
MyClass* pClass;
} _Item;
MyClass();
~MyClass();
void StartThread();
void* MyThread(void* arg);
typedef _Item Item;
Item MyItem;
static void _MyThread(void* lpParam)
{
Item* obj = (Item* )lpParam;
MyClass* a = obj->pClass;
a->MyThread(lpParam);
}
//private:
CAtlArray<item*> MyArray;
};
MyClass::MyClass()
{
}
void MyClass::StartThread()
{
HANDLE handle;
int val=0;
Item* MyNewItem = (Item* )malloc(sizeof(Item));
//wcscpy(MyNewItem.FullPath.GetBuffer(),L"c:\\");
MyNewItem->FullPath = new CString(L"c:\\");
MyNewItem->pClass = this;
handle = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)&_MyThread, MyNewItem, 0, NULL);
if ( handle == NULL) ExitProcess(0);
}
void* MyClass::MyThread(void* arg)
{
Item* newObj = (Item* )arg;
MyArray.Add(newObj);
return 0;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
MyClass *myClass = new MyClass();
myClass->StartThread();
Sleep(200);
CString str;
str.Format(L"\n\n--- PATH=%s --- \n\n",*(myClass->MyArray.GetAt(0)->FullPath));
OutputDebugString(str);
return 1;
}
</atldebugapi.h></atlcoll.h></atltime.h></atlbase.h>
最好的运气:D
Best of luck :D
这篇关于使用CAtlArray作为类成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!