使用CAtlArray作为类成员变量 [英] Using CAtlArray for a class member variable

查看:135
本文介绍了使用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.

推荐答案

可能是您错过了首先分配malloc的原因在插入MyArray之前.
MyDataStructure* pNewStruct;
pNewStruct =(MyDataStructure* )malloc(sizeof(MyDataStructure));
MyArray.Add(pNewStruct);


希望这会起作用.


Hope this will work.


这对我有用,我在线程退出后获得价值.与我比较您的代码,您可以理解您的问题.另外,CreateThread无法调用成员函数,因此您必须为此使用静态函数.
This work for me and i get value after thread exit. Compare your code with me , you can understand your problem. Another thing CreateThread can not call a member function, so you must need a static function for this.,

#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作为类成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆