1、 小弟我定义了一个简单的struct,以容纳多个变量。
2、 然后,以这样的一个新类型做元素,声明了一个vector。
3、 当使用此vector做元素插入的动作时,发生错误,错误为无此struct的拷贝构造函数可以使用。
4、 于是,再定义拷贝构造函数,以A(A&)的方式调用此拷贝构造函数成功。
5、 but,再次执行步骤3,仍旧显示原错误。
6、 源代码如下://test head file
#include <vector>
using namespace std; 
Struct   smplS
{
int i;
double d; smplS();
smplS(smplS &);
}//test Cpp file
smplS:: smplS():i(0),d(0.0)
{}smplS:: smplS(smplS & haha)
{
//do somthing
}main()
{
vactor<smplS> TestVector;
smplS TestS;
//……….Initial TestS
TestVector.push_back(TestS);}d:\program files\microsoft visual studio\vc98\include\xmemory(34) : error C2558: struct 'smplS' : no copy constructor available
        d:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(struct smplS *,const struct smplS &)' being compiled
Generating Code...
Error executing cl.exe. 
test.exe - 1 error(s), 0 warning(s)请各位大侠看一下,怎幺办? 
分数大大的。

解决方案 »

  1.   

    typedef vector<smplS, allocator<smplS> >TestVector;
      

  2.   

    thank you !
    try it just now!see you later!
      

  3.   

    struct AA
    {
    long ww;
    long ss;
    };
    vector<AA> tempArraye;
    AA temp;
    temp.ss =0;
    temp.ww = 1;
    tempArraye.push_back(temp);
    没有什么问题啊,我使用过,
    你看看你的结构体定义中少了个分号,是不是这样的问题!
    使用时没有问题啊,
      

  4.   

    谢谢二位大侠的关注和帮助。
    我当时提问的时候呢,为了简化问题,在例程中定义的struct只包含了几个简单类型,所以,按照somewxf的代码执行是能够成功的。somewxf的答案正确,20分相赠,但是难以表达我的感激。另外也非常谢谢verybigbug的关注,谢谢。为什幺在我的真实代码执行会有错误呢?因为在我的真实代码中,我定义的struct中还包含了我定义的另外一个二维动态数组。问题就出在他的身上。他没有定义拷贝构造函数(我以为重载操作符=就足够了呢!)。但是编辑器只是追溯到struct而已,所以这个小问题,一直困扰我。总结一下通过这个问题的心得体会,还存在的疑问,请大家继续讨论或者指点:讨论:一个容器类型,例如A,的元素类例如B还包含服务类例如C,也就是说B还包含其它类型。那幺当A的对象执行新增元素操作的时候,会越过B的拷贝构造函数,却会调用到C的拷贝构造函数!为什幺?以怎样的过程呢?
      

  5.   

    I' can't send my $ to "somewxf",for need admin's permission?
    please administrator give my 20 percent to "somewxf",or tell me how to do these!TKS.
      

  6.   

    那你就自己写拷贝构造函数:
    //test head file
    #include <vector>
    using namespace std; 
    Struct   smplS
    {
    int i;
    double d; smplS();
    smplS(smplS &);
    smplS(smplS const &o);
    }