【问题标题】:How to call the same function with different datatype?如何使用不同的数据类型调用相同的函数?
【发布时间】:2015-07-19 13:11:05
【问题描述】:

这是一个可以用不同数据类型调用的类:

template<class TDataType> 
void SetProperties(IndexType PropertiesId, 
                   const Variable<TDataType>& rVariable, 
                   const TDataType& Value)
{
    mpModeler->SetProperties(PropertiesId, rVariable, Value);
}

其中 Modeler::SetProperties 定义为:

template<class TDataType> 
void SetProperties(IndexType PropertiesId, 
                   const Variable<TDataType>& rVariable, 
                   const TDataType& Value)
{
    if (mpModel->GetProperties(PropertiesId).get() == 0)
    {
        mpModel->AddProperties(PropertiesId, Properties::Pointer(new Properties(*mpModel)));
    }

    PropertyFunction<TDataType>::Pointer constant_property(new ConstantProperty<TDataType>(Value));

    mpModel->GetProperties(PropertiesId)->SetProperty(rVariable, constant_property);
}

SetProperties 类被调用:

yyvsp[0].statement_handler->Execute(mpKernel); 

Execute() 定义为:

template<class TDataType> class GeneratePropertiesStatement : public Statement
{
public:

    GeneratePropertiesStatement(int Id, 
                                const Kratos::Variable<TDataType>& rVariable, 
                                const TDataType& Value) : mId(Id), 
                                                         mVariable(rVariable), 
                                                         mValue(Value){}

    void Execute(Kratos::Kernel* pKernel)
    {
        pKernel->SetProperties(mId, mVariable, mValue);
    }

    int mId;
    Kratos::Variable<TDataType> mVariable;
    TDataType mValue;
};

单个数据或多个数据通过以下语句传递给Value:

单个数据:

yyval.statement_handler = new GeneratePropertiesStatement<double>(yyvsp[-4].integer_value, *yyvsp[-2].double_variable, yyvsp[0].double_value);

其中yyvsp[0].double_value 定义为double

多个数据:

yyval.statement_handler = new GeneratePropertiesStatement<Kratos::Vector<double> >(yyvsp[-4].integer_value, *yyvsp[-2].vector_double_variable, *yyvsp[0].vector_double_value);

其中*yyvsp[0].vector_double_value 定义为vector

但是,上面的实现依赖于一些外部数据,我需要直接调用函数SetProperties。我定义了以下参数并成功调用了该函数:

int i;
const Kratos::Variable<double>* double_variable;
double regionmapi;
...
pKernel->SetProperties(i, *double_variable, regionmapi);

但是,当我定义以下参数并调用函数来传递多个数据时,它失败了:

double tmp3[3];
std::vector<double>aa;
for (int i = 0; i < 3; i++)aa.push_back(tmp3[i]);
pKernel->SetProperties(i, *double_variable, aa);

谁能帮我看看?

【问题讨论】:

  • 类型在 C++ 中很重要。如果您需要使用不同的类型调用它,您有两种选择:1. 使用派生自预期类型的​​类型传递变量 2. 创建将传递的类型转换为预期类型的​​新方法并调用原始方法
  • 你能发布你看到的错误吗?

标签: c++ types arguments


【解决方案1】:

在使用多个数据调用SetProperties 时,第二个参数必须是Kratos::Variable&lt;std::vector&lt;double&gt;&gt; 类型,以便与第三个参数类型std::vector&lt;double&gt; 保持一致。从您显示的代码来看,第二个参数的类型似乎是Kratos:Variable&lt;double&gt;,这与第三个参数冲突。

【讨论】:

  • 感谢您的热情回复。但是原代码是可以的,单数据(yyvsp[0].double_value)和多数据(*yyvsp[0].vector_double_value)都可以传。
猜你喜欢
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多