评估字符串到类变量 [英] Evaluate string to class variable

查看:132
本文介绍了评估字符串到类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要评估一个字符串,以便我可以为类变量赋值:



类:

  class DATACLASS {
public:
double variable1,variable2,variable3;
};

void init():

  void init()
{
DATACLASS * d = new DATACLASS;
std :: string ssList [3] = {variable1,
variable2,
variable3};
for(unsigned i = 0; i <3; ++ i)
{
std :: stringstream ss;
ss<< ssList [i];
//ss.str()。c_str()对应于variable1,variable2,variable3
mxArray * array_ptr = mexGetVariable(base,ss.str ));
double pr =(double)mxGetPr(array_ptr)[0];
//如何在这里做同样的事情?
//我想要评估的东西
// data-> ssList [i] = pr;
//或更精确地
// data-> variable1 = pr;
//但是从下面的ss字符串(但这不起作用)
data-> ss.str()。c_str()= pr;
}

当尝试这样做时会出现这种错误: p>

 错误C2039:'ss':不是'DATACLASS'的成员
pre>

解决方案

没有大量努力,最合理的方法就是下面这样。你可以使用宏,函数,容器,模板,指针/引用等抽象出一些东西,但这是基本的要点。我不建议这样做,承诺时间,除非你有一个令人信服的理由。您的最终目标是什么?

  class DATACLASS {
public:
double variable1,variable2,variable3;
};

void init()
{
DATACLASS * d = new DATACLASS;
std :: string ssList [3] = {variable1,
variable2,
variable3};
for(unsigned i = 0; i <3; ++ i)
{
std :: stringstream ss;
ss<< ssList [i];
//ss.str()。c_str()对应于variable1,variable2,variable3
mxArray * array_ptr = mexGetVariable(base,ss.str ));
double pr =(double)mxGetPr(array_ptr)[0];
if(ss.str()==variable1)
data-> variable1 = pr;
else if(ss.str()==variable2)
data-> variable2 = pr;
else if(ss.str()==variable3)
data-> variable3 = pr;
}
}


I need to evaluate a string so i can assign a value to a class variable :

Class :

class DATACLASS {
public:
    double variable1, variable2, variable3;
};

The void init() :

void init() 
{
    DATACLASS *d = new DATACLASS;
    std::string ssList[3] = {"variable1", 
                             "variable2", 
                             "variable3"};                        
    for(unsigned i = 0; i < 3; ++i)
    {
      std::stringstream ss;
      ss << ssList[i];
      //ss.str().c_str() correspond to "variable1", "variable2", "variable3"
      mxArray *array_ptr = mexGetVariable("base", ss.str().c_str());
      double pr = (double)mxGetPr(array_ptr)[0];
      // How to make the same thing here?
      // I would like to have something that would evaluate
      // data->ssList[i] = pr;
      // or more precisely      
      // data->variable1 = pr;
      // but from the ss string like below (but this doesn't work)
      data->ss.str().c_str() = pr;
    }

I get this kind of error when trying to do it this way :

error C2039: 'ss' : is not a member of 'DATACLASS'

解决方案

The closest you'll reasonably come without a huge amount of effort is something like the following. You could abstract away some things using macros, functions, containers, templates, pointers/references, etc., but this is the basic gist. I wouldn't suggest doing this and committing the time to it unless you have a compelling reason. What is your end goal?

class DATACLASS {
public:
    double variable1, variable2, variable3;
};

void init() 
{
    DATACLASS *d = new DATACLASS;
    std::string ssList[3] = {"variable1", 
                             "variable2", 
                             "variable3"};                        
    for(unsigned i = 0; i < 3; ++i)
    {
      std::stringstream ss;
      ss << ssList[i];
      //ss.str().c_str() correspond to "variable1", "variable2", "variable3"
      mxArray *array_ptr = mexGetVariable("base", ss.str().c_str());
      double pr = (double)mxGetPr(array_ptr)[0];
      if(ss.str() == "variable1")
        data->variable1 = pr;
      else if(ss.str() == "variable2")
        data->variable2 = pr;
      else if(ss.str() == "variable3")
        data->variable3 = pr;
    }
}

这篇关于评估字符串到类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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