如何在 .ned 文件中声明参数向量 [英] How to declare a vector of parameters in .ned file

查看:66
本文介绍了如何在 .ned 文件中声明参数向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 INET 的 .ned 文件中声明一个向量.

I am trying to declare a vector in .ned file in INET.

我尝试了以下方式,但出现语法错误.

I tried in following way but got syntax error.

double new[] @unit(s) = default({0.01s, 0.02s, 0.05s, 0.08s, 0.003s});

有人可以建议我正确的语法吗?

Would anyone please suggest me the correct syntax?

我使用的是带有 INET 4 框架的 OMNET++ 5.5.1.

I'm using OMNET++ 5.5.1 with INET 4 framework.

谢谢.

推荐答案

您无法在 NED 中声明参数向量.只有模块可以声明为向量.
但是,您可以使用字符串实现类似的效果.
在 NED 中声明假装向量的字符串:

You couldn't declare a vector of parameters in NED. Only modules may be declared as vector.
However, you may achieve similar effect using string.
In NED declare the string that pretends a vector:

string times = default("0.01s 0.02s 0.05s 0.08s 0.003s");

然后添加模块的 C++ 类,例如在 initialize() 中的代码:

Then add in C++ class of your module, for example in initialize() that code:

std::vector<std::string> timesStr = cStringTokenizer(par("times")).asVector();
std::vector<simtime_t> timesPar;  
for (auto i : timesStr) {
    simtime_t t = simtime_t::parse(i.c_str());
    timesPar.push_back(t);
}

EV << "Read " << timesPar.size() << " parameters from \"times\"" << std::endl;
// show all read values
for (int j=0; j<timesPar.size(); ++j) {
   EV << timesPar[j] << "; ";
}

执行后timesPar包含来自times的参数数组.

After executing timesPar contains the array of parameters from times.

注意事项:

  • 在此示例中,空格是分隔符,但可以使用任何其他字符作为分隔符 cStringTokenizer.
  • new 是 C++ 中的关键字,因此我建议为参数和变量使用其他名称.
  • In this example a space is the delimiter, but it is possible to use any other character as delimiter in cStringTokenizer.
  • new is a keyword in C++ therefore I suggest using other name for parameter and variable.

这篇关于如何在 .ned 文件中声明参数向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆