【发布时间】:2011-02-17 11:52:20
【问题描述】:
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 不同的另一个数据结构)
谢谢
【问题讨论】:
-
您可以(并且应该)通过 const 引用将您的向量传递给
print。此外,FeatureXD类声明中缺少继承语句。 -
@Stefan - 请不要建议对问题中发布的代码进行编辑。很可能任何错误都是 OP 看到的问题的原因。
-
是的,当然,我在想什么 :)
标签: c++ inheritance vector