After not using C++ since college, I'm trying to use a vector with 2 types of child objects, and I've obviously gotten something wrong.
Initially I used a vector of pointers, which worked, but if I understand correctly, that will leak memory when cleared.
The error I'm getting led me to believe that it had something to do with a static counter in the class (destroyed with last member?), but removing that has not solved it.
Error leads here, in stl_construct.h:
#if __cplusplus >= 201103L
// A deleted destructor is trivial, this ensures we reject such types:
static_assert(is_destructible<_Value_type>::value,
"value type is destructible");
#endif
Well, OK, but my destructors are all explicitly declared. I remembered the parent class should use a virtual destructor and fixed that, but the problem remains all the same.
Moving the constructor/destructor to public on the virtual parent class shouldn't (and indeed didn't) change things.
Right now I'm assuming that I'm misusing the vector somehow. Here's my example:
main:
#include <stdio.h>
#include "Foo.h"
#include <iostream>
Bar buildBar();
int main(int argc, char **argv)
{
std::vector<Foo> Foos;
Foos.reserve(1);
Foos.push_back(buildBar());
for (int idx=(0);sizeof(Foos);idx++)
{
try {
std::cout << "x = " << Foos.at(idx).getLoc().at(0);
}
catch (std::exception& e) {
std::cout << idx << ": Index out of range" << std::endl;
}
}
Foos.clear();
return 0;
}
Bar buildBar()
{
Bar* temp = new Bar(5,6);
return *temp;
}
Foo.h, with the constructor moved to header:
#ifndef FOO_H
#define FOO_H
#include <vector>
class Foo
{
public:
//int maxoID(){return lastoID;} //0-indexed
/* Other things */
virtual std::vector<int> getLoc(){ return {x_Base,y_Base};}
Foo():
//oID(-1),
x_Base(-1),
y_Base(-1){}
virtual ~Foo(){}
protected:
int x_Base;
int y_Base;
};
class Bar : public Foo
{
public:
Bar(int x1, int y1):
x_End(-1),
y_End(-1)
{
x_Base = x1;
y_Base = y1;
}
~Bar(){}
std::vector<int> getLoc() {return {x_Base,y_Base,x_End,y_End};}
protected:
int x_End;
int y_End;
};
#endif // FOO_H