gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:11:35 25 4
gpt4 key购买 nike

我有一个模板类,它使用一个策略作为它的输出和另一个模板参数来确定它的数据成员的类型。此外,构造函数采用指向存储在私有(private)指针中的基类的指针。 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 是一个接口(interface),应该被实现。它看起来像这样:

class ShapeGenerator {
public:
void generateShape (ModelCreator* m) = 0;
};

如果我用 g++ 4.3.4 (cygwin) 编译它,我会在 ShapeGenerator::generateShape 中得到一个错误,说 'ModelCreater' is not a type。我放入了 ModelCreator 的前向声明,但它没有任何改变。我尝试了一些类型和参数的组合,例如只传递 vector ,然后我收到一条错误消息,其中说明类型不完整。我想这就是问题所在。

那么,是否可以在没有特定参数的情况下传递模板化类型?如果是,怎么办?

编辑:我不受 ModelCreator 类型名称的约束。如果我必须写更多的模板,这不是问题。但我不想在 ShapeCreator 对象中指定 ModelCreator 的类型。这可能吗?

编辑2:好吧,我想我对这个“设计”有点乐观。加入一些配料并喝汤就好了。但是现在盐必须知道锅里的水的种类。我会将模板更改为普通的旧组合。谢谢你们。

最佳答案

如果您想使用带有“免费”模板参数的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; 

关于c++ - 作为函数参数的类型不完整?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com