作为函数参数的类型不完整?
我有一个模板类,它使用策略作为其输出,并使用另一个模板参数来确定其数据成员的类型。此外,构造函数采用指向存储在私有指针中的基类的指针。 this 对象的函数应采用指向模板类的 this 指针,以便它们访问数据。在代码中,它看起来像这样:
class ShapeGenerator;
template <typename PointData, typename OutputPolicy> class ModelCreator {
private:
OutputPolicy output;
ShapeGenerator* shape
std::vector<PointData> data;
public:
ModelCreator (ShapeGenerator *s) : shape(s) { }
void createShape() { shape->generateShape(this); }
};
ShapeGenerator 是一个接口,应该被实现。它看起来像这样:
class ShapeGenerator {
public:
void generateShape (ModelCreator* m) = 0;
};
如果我使用 g++ 4.3.4 (cygwin) 编译它,我会在 ShapeGenerator::generateShape 中收到错误,指出 'ModelCreater' 不是类型
。我放入了 ModelCreator 的前向声明,但它没有改变任何东西。我使用了一些类型和参数的组合,例如仅传递向量,然后我收到一条错误消息,其中说明了有关不完整类型的信息。我想这就是问题所在。
那么,是否可以在没有特定参数的情况下传递模板类型?如果是这样,怎么办?
编辑: 我不受 ModelCreator 类型名的约束。如果我必须像模板一样编写它,这不是问题。但我不想在 ShapeCreator 对象中指定 ModelCreator 的类型。这可能吗?
编辑2: 好吧,我想我对这个“设计”有点过于乐观了。如果加入一些配料并喝汤就好了。但现在盐必须知道锅里的水的种类。我会将模板更改为普通的旧构图。谢谢你们。
I have that template class that uses a policy for it's output and another template argument to determine the type for it's data members. Furthermore the constructor takes pointers to base classes which are stored in private pointers. Functions of this objects shall take a this pointer to the template class to give them access to the data. In code this looks like this:
class ShapeGenerator;
template <typename PointData, typename OutputPolicy> class ModelCreator {
private:
OutputPolicy output;
ShapeGenerator* shape
std::vector<PointData> data;
public:
ModelCreator (ShapeGenerator *s) : shape(s) { }
void createShape() { shape->generateShape(this); }
};
ShapeGenerator is an interface and shall be implemented. It looks like this:
class ShapeGenerator {
public:
void generateShape (ModelCreator* m) = 0;
};
If I compile this with g++ 4.3.4 (cygwin) I get an error in the ShapeGenerator::generateShape saying 'ModelCreater' is not a type
. I put in a forward declaration of ModelCreator but it changed nothing. I played with some combinations of types and parameters, for example passing only the vector and then I got an error message that said something about incomplete types. I guess this is the problem here.
So, is it possible to pass a templated type with without specific arguements? If so, how?
edit:
I'm not bound to the ModelCreator typename. If I have to write it more template-like this isn't problem. But I would like not to specify the types of ModelCreator in the ShapeCreator object. Is that possible?
edit2:
Ok, I guess I was a bit to optimistic with this "design". It would have been nice to just throw in some ingrediences and get a soup. But now the salt has to know about the kind of water in the pot. I'll change the templates to plain old composition. Thanks you guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想将
ModelCreator
与“免费”模板参数一起使用,那么您也必须将ShapeGenerator
设为模板:或者
第二个版本采用另一个模板作为参数。你可以像这样使用它:
If you want to use the
ModelCreator
with "free" template parameters, then you have to makeShapeGenerator
a template too:or
The second version takes another template as a parameter. You would use it like this:
您还需要使 ShapeGenerate 成为模板类:
You need to make ShapeGenerate a template class as well: