如果你的类是CObject的派生类,数组得用CObArray。
如果是其它的类,那么可以用CPtrArray来存储它的指针。

解决方案 »

  1.   

    CArray不能存储类只能存储它的指针。
    你可改为
    CObArray array;
    array.Add(new CMyClass);
      

  2.   

    CArray只是一个模版,不能直接用。你可以用CObArray或CPtrArray
      

  3.   

    你怎么使用CArray的?
    应该这样:
    CArray<CMyClass, CMyClass&> myArray;
    CMyClass temp;
    myArray.Add(temp);
      

  4.   

    CArray 应该可以直接用,如我最近刚用过的:
    CString str;
    CArray<CString, CString> arr; 
    arr.Add(str);
    这样把字符串加到数组中是没有问题的。
    我想问题还是出在你的CMyClass里的另一数组,
    你类里的另一数组成员去掉应该没问题吧?
      

  5.   

    建议用指针:
    CArray<CMyClass*, CMyClass*> myArray;
    CMyClass temp;
    myArray.Add(&temp); 可以省去类拷贝的麻烦。
     
      

  6.   

    // 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()
    {
      

  7.   

    你一定要定义CArray<CMyClass, CMyClass&> m_myArray. 然后使用m_myArray.Add(temp)
      

  8.   

    我当然是用模板参为CMyClass的呀。我是为了方便,就这么写的啦。
    还有,CArray绝对可以存储一般的类。
    我的程序里也有用CArray <CString,CString>m_SubsetName的形式。它不会报错。但到了我说的那个类就
    报错了。
    而且,很奇怪的是,一般模板参传的是<CMyClass,CMyClass &>,但我这样做的话,就会在afxtempl.cpp的
    SetAtGrow函数里出错。所以,我就改为用<CMyClass,CMyClass>但就出了开始说的那个情况。
      

  9.   

    Hidy,谢谢你的提醒。
    我做了个实验,发现真的是不管什么类,都可以直接用的。但要是这个类里又加了一个数组成员
    的话,就会报"No copy constructor available for class 'B'"这个错了.
    但怎样才能避免呢.我估计可能是编译器不知道怎么做动态数组的copy工作.(所以,我的类里有动态数组,编译器就报错了.但问题是,我写了个拷贝建构子,又报那个错了.真是奇怪.)
    各位大虾,最好能写个拷贝建构子给我看看.
    我的试验代码是:#include "afxtempl.h"struct A
    {
    int i;
    int j;
    };class B
    {
    public:
        CArray <A,A> m_AList;
    };class C
    {
    public: CArray <B,B>m_BList;
    };int main()
    {
    C c;
    B b;
    c.m_BList.Add(b);
    return 1;
    }
    这样就报"No copy constructor available for class 'B'"要是给它加上
    B::B(B &b)
    {
      m_AList.RemoveAll();

    就报'B' : no appropriate default constructor available我实在是不知道一个CArray对象创建出来后还能怎样拷贝?没有意义啊,我根本就没有数据往里面存啊.
    请大家帮忙.问题解决了,我再送100分.(呵呵,现在没有,我可用分是0了).
      

  10.   

    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()
    {

      

  11.   

    写这么多代码烦不烦哪!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!这么说:
    CArray subarray;
    CArray< subarray , subarray & > perarray;
    perarray 是不能用Add方法的.因为subarray没有operator =方法.You know ?
    但可以这样用: perarray.SetSize( perarray.GetSize()+1 );
    perarray[perarray.GetSize()-1)= CArray user;.....