c++ - Access Violation trying to read vector filled with objects -


edit: made changes , updated code in post based on comments of kyle , dieter, fixed clone()function , added assignment-operator fulfil rule of three. while fixes sure badly needed, same error prevails. maybe assignment operator wrong?

i'm using library jsonplus found online bigger project. need save objects of class cjsonarray in vector. cjsonarray comes no copy-constructor has pointer attribute, tried make 1 myself (first time made copy-constructor, i'm new c++).

here relevant part of cjsonarray:

cjsonarray.h

    class cjsonarray : public cjsonvalue     {     private:       std::vector <cjsonvalue*> members;     public:         lib_pre cjsonarray();         lib_pre cjsonarray(const cjsonarray * value);         lib_pre cjsonarray(const cjsonarray &); //the added copy constructor         lib_pre cjsonarray& operator=(const cjsonarray&);         lib_pre ~cjsonarray(); 

cjsonarray.cpp

 cjsonarray::cjsonarray(const cjsonarray& ori) : members(ori.members.size()) {         (std::size_t = 0; < ori.members.size()-1; ++i)             members[i] =ori.members[i]->clone();     } cjsonarray& cjsonarray::operator=(const cjsonarray& ori){     (std::size_t = 0; < ori.members.size() - 1; ++i){         this->members[i] = ori.members[i]->clone();     }        return *this; } 

aditionally had implement clone() function in cjsonvalue, abstract class , classes derived it. here relevant code snippets:

cjsonvalue.h

enum cjsonvaluetype {   jv_string,   jv_number,   jv_object,   jv_array,   jv_null,   jv_bool };  class cjsonvalue { private:   cjsonvaluetype type; public:   lib_pre cjsonvalue();   lib_pre virtual ~cjsonvalue();   lib_pre cjsonvalue(cjsonvaluetype type);   lib_pre virtual cjsonvalue * clone(); //the added clone-function   lib_pre virtual jstring tostring() const = 0;   lib_pre cjsonvaluetype gettype() const;   lib_pre virtual void clear(){}; }; 

cjsonvalue.cpp

    cjsonvalue * cjsonvalue::clone(){     return null; } 

example of derived class, cjsonvaluenumber.h

    class cjsonvaluenumber : public cjsonvalue { private:   int value; public:   lib_pre cjsonvaluenumber(int value);   lib_pre cjsonvaluenumber(const cjsonvaluenumber * value);   lib_pre cjsonvalue * clone();   lib_pre jstring tostring() const;   lib_pre void getvalue(int & number) const; }; 

cjsonvaluenumber.cpp

cjsonvalue * cjsonvaluenumber::clone(){     return new cjsonvaluenumber(*this); } 

main.cpp produces error:

 cjsonarray array1;  cjsonarray array2;  cjsonarray array3;  cjsonarray array4;  cjsonarray array5;   array1.addmember("test1");  array2.addmember("test1");  array3.addmember("test1");  array4.addmember("test1");  array5.addmember("test1");   arrays.push_back(array1);  arrays.push_back(array2);  arrays.push_back(array3);  arrays.push_back(array4);  arrays.push_back(array5);   std::string str = arrays[0].tostring(); 

error:

unhandled exception @ 0x026574bd in message.exe: 0xc0000005: access violation reading location 0x00000000.

debuginfo: on first push_back members of first entry corrupt. before implemented copy-constructor program crashed on second or third pushback, guess because vectore had realocate entries , not find them? sad, first time had deal problem.

so question is: did copy-constructor wrong? or totally on wrong way , has nothing it?

i looked @ many other stackoverflow questions dealing similar problems , tried follow advises there, guess did wrong along way.

please let me know if need provide additional information.

thanks in advance help!

cjsonvalue::clone() method needs virtual. if cjsonarray object stores collection of cjsonvalue objects (as opposed cjsonvaluenumber or otherwise) compiler can't know cjsonvaluenumber wants override clone() method if isn't virtual.

this lead cjsonvalue::clone() being called instead of cjsonvaluenumber::clone(). @ point have null references in array, lead access violation @ 0x00000000 (null defined 0, or 0x00000000).

since cjsonvalue abstract class anyway (cjsonvalue::tostring() method virtual void), make cjsonvalue::clone() virtual void well. guarantee clone method has implemented inheriting classes.

to summarize, change declaration of cjsonvalue::clone() to:

lib_pre virtual cjsonvalue * clone() = 0; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -