如何用不同的数据类型调用相同的函数?

时间:2022-11-17 19:12:32

Here is a class which can be called with different datatype:

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

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

where Modeler::SetProperties is defined as:

其中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);
}

The class SetProperties is called by:

类SetProperties由以下方法调用:

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

Where Execute() is defined by:

其中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;
};

Single data or multiple data are passed to Value by the following statements:

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

single data:

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

where yyvsp[0].double_value is defined as double;

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

multiple data:

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

where *yyvsp[0].vector_double_value is defined as a vector.

其中* yyvsp [0] .vector_double_value被定义为向量。

However, the above realization rely on some external data and I need to call the function SetProperties directly. I defined the following argument and successfully called the function:

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

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

However, when I defined the following argument and called the function to pass multiple data, it failed:

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

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);

Could anyone help me to take a look at it?

任何人都可以帮我看一下吗?

1 个解决方案

#1


In your call to SetProperties using multiple data, the second argument must be of type Kratos::Variable<std::vector<double>>, in order to be consistent with the third argument, which is of type std::vector<double>. From the code you show, it looks like the second argument is of type Kratos:Variable<double>, which conflicts with the third argument.

在使用多个数据调用SetProperties时,第二个参数必须是Kratos :: Variable >类型,以便与第三个参数一致,类型为std :: vector 。从您显示的代码中,看起来第二个参数的类型为Kratos:Variable ,它与第三个参数冲突。

#1


In your call to SetProperties using multiple data, the second argument must be of type Kratos::Variable<std::vector<double>>, in order to be consistent with the third argument, which is of type std::vector<double>. From the code you show, it looks like the second argument is of type Kratos:Variable<double>, which conflicts with the third argument.

在使用多个数据调用SetProperties时,第二个参数必须是Kratos :: Variable >类型,以便与第三个参数一致,类型为std :: vector 。从您显示的代码中,看起来第二个参数的类型为Kratos:Variable ,它与第三个参数冲突。