无法将对象推回矢量

时间:2018-11-23 10:01:07

标签: c++ vector gsoap

错误: 调用代码段时出现错误 引发异常:读取访问冲突。 std :: _ Vector_alloc>> :: _ Myend(...)返回0xC。

bool _Has_unused_capacity() const _NOEXCEPT
{   // micro-optimization for capacity() != size()
return (this->_Myend() != this->_Mylast());
}

代码段:             错误: 调用代码段时出现错误 引发异常:读取访问冲突。 std :: _ Vector_alloc>> :: _ Myend(...)返回0xC。

bool _Has_unused_capacity() const _NOEXCEPT
{   // micro-optimization for capacity() != size()
return (this->_Myend() != this->_Mylast());
}


class  A 
{
public:
    class B *objTRANSACTIONID;
};
// second class
class  B {
public:
    std::vector<class C * >TRANSACTIONID;
};
// Third class
class C {
public:
    std::string *APPLICATIONID;
    std::string *CREATEDBY;
};
//  C++ Code
int main()
{
    A *aObject =new A();
    C *DNINFO = new C();
    std::string FieldName = "UserType";
    DNINFO->APPLICATIONID = &FieldName;
    DNINFO->CREATEDBY = &FieldName;
    // Throws error as “unused capacity” for vector.
    aObject->objTRANSACTIONID->TRANSACTIONID.push_back(DNINFO);
}

1 个答案:

答案 0 :(得分:0)

您的A对象具有未初始化的objTRANSACTIONID,没有B对象可将DNINFO推入TRANSACTIONID

请不要在此处使用指针和new

// Third class
class C {
public:
    std::string APPLICATIONID;
    std::string CREATEDBY;
};

// second class
class  B {
public:
    std::vector<C>TRANSACTIONID;
};

class  A 
{
public:
    B objTRANSACTIONID;
};


//  C++ Code
int main()
{
    A aObject;
    C DNINFO;
    std::string FieldName = "UserType";
    DNINFO->APPLICATIONID = FieldName;
    DNINFO->CREATEDBY = FieldName;
    aObject.objTRANSACTIONID.TRANSACTIONID.push_back(DNINFO);
}

显然,您的类是工具生成的,因此在这种情况下,您必须分配一个B。请注意,此工具应告诉您何时(或是否)delete。如果没有,请扔掉该工具,自己编写明智的类。

//  C++ Code
int main()
{
    A  aObject;
    aObject.objTRANSACTIONID = new B;
    C * DNINFO = new C;
    DNINFO->APPLICATIONID = new std::string("UserType"); // have to assume owning raw pointer
    DNINFO->CREATEDBY = new std::string("UserType"); // have to assume owning raw pointer
    aObject.objTRANSACTIONID->TRANSACTIONID.push_back(DNINFO);
}