有如下声明
struct A{
......
};
struct B{
.......
CArray <A,A> TA;
};
CArray <B,B> TB;
A tempA;
B tempB;
TA.Add(A);
TB.Add(B);出现错误,前一个Add没有问题,但是后一个Add则报错。
error C2664: 'Add' : cannot convert parameter 1 from 'struct State' to 'struct State'
No copy constructor available for struct 'State'
请高手指正
struct A{
......
};
struct B{
.......
CArray <A,A> TA;
};
CArray <B,B> TB;
A tempA;
B tempB;
TA.Add(A);
TB.Add(B);出现错误,前一个Add没有问题,但是后一个Add则报错。
error C2664: 'Add' : cannot convert parameter 1 from 'struct State' to 'struct State'
No copy constructor available for struct 'State'
请高手指正
解决方案 »
- 派生于基类CMDIChildWndEx的CChildFrame类不绘制子窗口?
- 求助一个简单问题
- 请教高手,关于OnTimer()函数串口读写之间的衔接问题
- OpenGL 光源、颜色问题
- 关于匿名管道的问题
- 急,急,急,百分求 多播 聊天室程序vc原代码!
- 新一代网络动画软件出来了,全面代替 flash 请看 www.svgcn.com
- dialog程序如何在启动时自动最大化?
- dll注入进程为什么不行?
- int fun(const int ¶) 中的&是为了传引用,使效率更高,而const是保证传进去的引用不可被修改。相当于int fun (int para)的增强版!!我说的对吗??
- 急!如何用MCI命令播放ASF?
- 工作线程占用CPU时间的问题
support.microsoft.com/?kbid=231995
B(const B& b){
// Your copy ctor body goes here.
}
--------------------------------------------------------------------------------
The information in this article applies to: Microsoft Visual C++, 32-bit Enterprise Edition 5.0, 6.0
Microsoft Visual C++, 32-bit Professional Edition 5.0, 6.0
Microsoft Visual C++, 32-bit Learning Edition 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 that are listed at the beginning of this article.This problem was corrected in Microsoft Visual C++ .NET.
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()
{
} Additional query words: Keywords: kbCPPonly kbVC500bug kbVC600bug kbNoUpdate
Issue Type: kbbug
Technology: kbVCsearch kbAudDeveloper kbVC500 kbVC600 kbVC32bitSearch kbVC500Search --------------------------------------------------------------------------------Send feedback to Microsoft© 2002-2003 Microsoft Corporation. All rights reserved.