class IFeature
{
public:
virtual std::string string() = 0;
};
class Feature2D
{
public:
virtual std::string string() { .... }
};
class Feature3D
{
public:
virtual std::string string() { .... }
};
void print(std::vector<IFeature*> & v)
{
for (size_t i=0; i<v.size(); i++)
std::cout << v[i]->string() << std::endl;
}
void main()
{
std::vector<Feature2D*> v2d;
// push...
print(v2d); // compile error
std::vector<Feature3D*> v3d;
// push...
print(v3d); // compile error
}
有关如何获得此打印功能的任何建议? (也许使用不同的数据结构std :: vector)在C++中继承对象的向量
谢谢
你可以(也应该)通过const引用将你的向量传递给`print`。此外,您的`FeatureXD`类声明中缺少继承声明。 – 2011-02-17 12:02:31
@Stefan - 请不要对发布在代码中的代码提出修改建议。可能是因为任何错误都是OP看到问题的原因**。 – ChrisF 2011-02-17 12:47:55
是的,当然,我在想什么:) – 2011-02-17 14:11:45