作为函数参数的类型不完整?

发布于 2024-09-08 16:26:18 字数 1105 浏览 6 评论 0原文

我有一个模板类,它使用策略作为其输出,并使用另一个模板参数来确定其数据成员的类型。此外,构造函数采用指向存储在私有指针中的基类的指针。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高跟鞋的旋律 2024-09-15 16:26:18

如果您想将 ModelCreator 与“免费”模板参数一起使用,那么您也必须将 ShapeGenerator 设为模板:

template <typename PointData, typename OutputPolicy> 
class ShapeGenerator {
public:
    void generateShape (ModelCreator<PointData,OutputPolicy>* m) = 0;
};

或者

template <template <typename, typename> class ModelCreator> 
class ShapeGenerator {
public:
    void generateShape (ModelCreator* m) = 0;
};

第二个版本采用另一个模板作为参数。你可以像这样使用它:

ShapeGenerator<ModelCreator<PointDataType,OutPutPolicyType> > shapeGenerator; 

If you want to use the ModelCreator with "free" template parameters, then you have to make ShapeGenerator a template too:

template <typename PointData, typename OutputPolicy> 
class ShapeGenerator {
public:
    void generateShape (ModelCreator<PointData,OutputPolicy>* m) = 0;
};

or

template <template <typename, typename> class ModelCreator> 
class ShapeGenerator {
public:
    void generateShape (ModelCreator* m) = 0;
};

The second version takes another template as a parameter. You would use it like this:

ShapeGenerator<ModelCreator<PointDataType,OutPutPolicyType> > shapeGenerator; 
天生の放荡 2024-09-15 16:26:18

您还需要使 ShapeGenerate 成为模板类:

template <typename PointData, typename OutputPolicy>
class ShapeGenerator;

template <typename PointData, typename OutputPolicy>
class ModelCreator {
private:
    OutputPolicy output;
    ShapeGenerator< PointData, OutputPolicy >* shape;    
    std::vector<PointData> data;
public:
    ModelCreator (ShapeGenerator< PointData, OutputPolicy >* s) : shape(s) { }
    void createShape();
};

template <typename PointData, typename OutputPolicy>
class ShapeGenerator {
public:
    void generateShape (ModelCreator< PointData, OutputPolicy> * m) = 0;
};

template <typename PointData, typename OutputPolicy>
void ModelCreator< PointData, OutputPolicy >::createShape() { shape->generateShape(this); }

You need to make ShapeGenerate a template class as well:

template <typename PointData, typename OutputPolicy>
class ShapeGenerator;

template <typename PointData, typename OutputPolicy>
class ModelCreator {
private:
    OutputPolicy output;
    ShapeGenerator< PointData, OutputPolicy >* shape;    
    std::vector<PointData> data;
public:
    ModelCreator (ShapeGenerator< PointData, OutputPolicy >* s) : shape(s) { }
    void createShape();
};

template <typename PointData, typename OutputPolicy>
class ShapeGenerator {
public:
    void generateShape (ModelCreator< PointData, OutputPolicy> * m) = 0;
};

template <typename PointData, typename OutputPolicy>
void ModelCreator< PointData, OutputPolicy >::createShape() { shape->generateShape(this); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文