有如下声明
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'
请高手指正

解决方案 »

  1.   

    应该是结构体没有复制构造函数造成的。改成用class试试
      

  2.   

    Q231995 FIX: Error C2664 and C2582 While Using CArray Class 
    support.microsoft.com/?kbid=231995 
      

  3.   

    应该是struct B的原因,这个结构里面是否又嵌有结构,联合或类等.而A却没有,A就没问题,你查一查.
      

  4.   

    对不起,是构造一个带有自身引用参数的构造函数:
    B(const B& b){
    // Your copy ctor body goes here.
    }
     
      

  5.   

    Knowledge Base  FIX: Error C2664 and C2582 While Using CArray ClassPSS ID Number: Q231995Article Last Modified on 02-11-2002
    --------------------------------------------------------------------------------
    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&copy; 2002-2003 Microsoft Corporation. All rights reserved.
      

  6.   

    to Soundboy(小猫):结构和类的主要区别就是因为结构不存在函数吧?不知道如何能在结构中构造一个构造函数呢?不好意思,不知道是我知道的比较片面,还是您弄错了,可否给仔细说明一下呀?
      

  7.   

    to  pomelowu(羽战士)我把结构改成类之后,当然也有了构造函数和析构函数,还是同样的错误
      

  8.   

    首先,struct也可以有构造函数和成员函数的。其次,需要的是拷贝构造函数,就是Soundboy(小猫)说的“构造一个带有自身引用参数的构造函数”
      

  9.   

    谢谢jiangsheng(蒋晟.MSMVP2004Jan),Soundboy(小猫) ,pomelowu(羽战士) ,根据三位的帮助,我已经知道解决办法了。我已经给三位分数了,多谢!