请教高手,这段代码可以这行吗?(在线等,急!!!!!!)

wangwenhong1000 2004-11-26 10:21:28
class thread_str
{
public:
DWORD ThreadID;
CArray<char * ,char *> ptParams;
CArray<char * ,char *> ptCols;
int flag;
}

CArray<thread_str,thread_str> ptThread;
void InitThread()
{
thread_str thread;
ThreadID=1234;
for(int i=0;i<100;i++)
{
thread.ptParams.SetSize(thread.ptParams.GetSize()+1,-1);
thread.ptCols.SetSize(thread.ptCols.GetSize()+1,-1);
strcpy(thread.ptParams[i],"kjfdsjsdjflskdjflkds");
strcpy(thread.ptCols[i],"lkjfdkasjflksdjf");
ptThread.Add(thread);

}
}

麻烦各位帮我看下,如果不行的话,有什么好的解决方法吗?我现在就要想把一个类的对象写如数组,
对于类中的数组的动态的添加。谢谢
...全文
95 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingzai 2004-11-26
  • 打赏
  • 举报
回复
CArray need implicit copy constructor .

BUG: Error C2664 and C2582 While Using CArray Class

Q231995


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Visual C++, 32-bit Editions, versions 5.0, 6.0

--------------------------------------------------------------------------------


SYMPTOMS
When a user-defined class contains a CArray and the same user-defined class is nested in another class, you may get the following errors if no copy constructor and assignment operator are provided for the class:

main.cpp(52): error C2664: 'Add' : cannot convert parameter 1 from 'class B' to 'class B' No copy constructor available for class 'B'
After adding a copy constructor, you get the following error message:
afxtempl.h(443): error C2582:'B' : 'operator =' function is unavailable afxtempl.h(1566):while compiling class-template member function 'void __thiscall CArray<class B,class B>::SetAtGrow(int,class B)'



CAUSE
If the class that contains a CArray is nested in another class, then its objects must be copied.

The compiler does not construct an implicit copy constructor and copy assignment operator because the class in question has CArray as a member, which does not have a copy constructor and copy assignment operator, and CArray inherits from CObject, which has a protected copy construtor and a copy assignment operator. The compiler tries to generate the implicit ones, but that generates a call to CObject's version of them. Because they are protected, the above errors are generated.



RESOLUTION
You need to provide a copy constructor and an assignment operator for the class.



STATUS
Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.



MORE INFORMATION
The following example shows the correct use of the CArray class:


// No compiler option needed.
#include <afxtempl.h>

struct A
{
int i;
int j;
};

class B
{

public:
B();
~B();

// Need to define copy ctor and assignment operator.

B(const B& b){
// Your copy ctor body goes here.
}

/* const B& */ void operator= (const B& b) {
// Your assignment operator body goes here.
}

protected:
CArray<A, A> arrayA;
};

B::B(){}
B::~B(){}

class C
{

public:
C();
~C();
void addElement();

protected:
CArray<B, B> arrayB;
};

C::C(){}
C::~C(){}
void C::addElement()
{
B temp;
arrayB.Add(temp);
}

void main()
{
}
wangwenhong1000 2004-11-26
  • 打赏
  • 举报
回复
大哥,我再问下,如果是用typedef CTypedPtrList<CObList, thread_str*> Cthread_strList;
那么我的对象里的数组动态增加或减少,会有影响吗?
老夏Max 2004-11-26
  • 打赏
  • 举报
回复
参考MSDN中关于“CTypedPtrArray”的说明!!
老夏Max 2004-11-26
  • 打赏
  • 举报
回复
CArray<thread_str,thread_str&> ptThread;
老夏Max 2004-11-26
  • 打赏
  • 举报
回复
typedef CTypedPtrList<CObList, thread_str*> Cthread_strList;
wangwenhong1000 2004-11-26
  • 打赏
  • 举报
回复
我现在是执行ptThread.Add(thread)编译报错。
错误信息为:
error C2664: 'Add' : cannot convert parameter 1 from 'class thread_str' to 'class thread_str'
No copy constructor available for class 'thread_str'
老夏Max 2004-11-26
  • 打赏
  • 举报
回复
写成链表不是更好?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧