c++ returning a list of object pointers read from file -
I have this member function that lists the object pointer
list & lt; Product * & gt; Product :: loadProducsts () {ifstream file; The product is temporary; & Lt; Product * & gt; Products; File.open ("Cigarette DAT", IOS :: binary); If (file.is_open ()) {file.seekg (0, iOS :: end); Int size = (int) file.tellg (); File.seekg (0, ios :: beg); While (file.tellg () (& amp; temp), size (temp)); Product * R = new product; R = & amp; Temporary; Products.push_front (r); }} File.close (); Return product; } In my main function I call this function and then I would like to print some value of the item
...... List & lt; Product * & gt; S; ...... s = p.loadProducsts (); (List & lt; Product * & gt; :: iterator iter = s.begin (), end = s.end (); iter! = End; ++ iter) {cout & lt; & Lt; (* ITER) - & gt; GetModel (); } Now I can not see anything on the console and I have this error
"source / buildd / eglibc" source file -2.19 / signal /../ nptl / sysdeps / unix / sysv / linux / raise.c " and warn me of some time during execution:
Can not find the linker symbol for the virtual table for the 'product' value I think it's about free storage A problem in when the main execution is ending. Any suggestions?
Unlike compilation issues, here are some things: First of all, in your work Stack on the product example, you can create heap based product here: product * r = new product; But then you throw the object (leakage) by place of the pointer with the address of your stack based variable: r = & Amp; Temporary; So you repeated a collection of the same indicator repeated several times and many leaked pile objects.
Second, instead of just forced-casting binary data in your object, it would be better to make sure that your product objects properly and serial and deserialises so that you can simply write: product r; File & gt; & Gt; R; Products.push_front (r); Notice within my loop how do I use objects instead of raw pointers? If you have copies of copies very good and your object is not high enough, then efficiency loss will be compensated more than the reduction in vulnerabilities. If the signs are important to you, make sure that you try to use a smart pointer to reduce leakage. Good luck!
Comments
Post a Comment