为什么我的If-Else语句正常工作?
我有一个项目,必须根据我想要的磁场发生器的旋转场更改一些参数。我不是开发人员,C ++不是我的专家计划,但是我需要找到一种使用Toggle函数在两种不同配置之间进行更改的方法。我尝试使用if-else语句,但这不起作用。手动更改参数确实可以工作,因此我相信IF-ELSE可能不会加载或其他内容。任何意见都将不胜感激。
void ParticleControlPanel::processTimer()
{
//original field orientation (B-field aligned with z-axis)
tf::Vector3 field_orientation(0.,0.,1.);
//original rotiation axis; (rotation axis aligned with x-axis)
tf::Vector3 rot_axis(1.,0.,0.);
rot_axis.setX(cos(azimuth_/180.*pi));
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(0);
//toggle drill and cube
if (toggle_)
{
tf::Vector3 field_orientation(1.,0.,0.);
tf::Vector3 rot_axis(0.,0.,1.);
rot_axis.setX(0);
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(cos(azimuth_/180.*pi));
}
else
{
tf::Vector3 field_orientation(0.,0.,1.);
tf::Vector3 rot_axis(1.,0.,0.);
rot_axis.setX(cos(azimuth_/180.*pi));
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(0);
}
编辑:我在代码开头定义了toggle_函数。
namespace mag_control {
ParticleControlPanel::ParticleControlPanel(QWidget *parent) :
rviz::Panel(parent),
magfield_topic_("/desired_field"),
magfield_grad_topic_("/desired_field_grad"),
holding_lock_(false),
activated_(false),
controlling_(false),
wobble_(false),
toggle_(false),
frequency_(0.00),
azimuth_(0.0),
rot_angle_(0.0),
t_prev_(0.0),
pi(std::acos(-1)),
gradient_(0.,0.,0.),
position_(0.,0.,0.),
z_control_(false),
gradient_z_(0.0),
thresh_bin_(0),
thresh_hough_(0),
config_("demo.yaml")
{
编辑:我听说变量field_orientation和rot_axis在if-else内部和下方的变量不同。我该如何更改?同样,这是我没有很多经验的事情,但是由于工作电路,我需要修复。
void ParticleControlPanel::processCheckboxToggle(int value)
{
if(value){
toggle_ = true;
}
else {
toggle_ = false;
}
}
I have a project where I have to change some parametres depending on the field of rotation I want for a magnetic field generator. I am not a developer and c++ is not my program of expertice, but I need to find a way to change between two different configurations using a toggle function. I tried using an If-Else statement, but it doesn´t work. Changing the parameters manually does work, so I believe the if-else may not be loading or something. Any input would be greatly appreciated.
void ParticleControlPanel::processTimer()
{
//original field orientation (B-field aligned with z-axis)
tf::Vector3 field_orientation(0.,0.,1.);
//original rotiation axis; (rotation axis aligned with x-axis)
tf::Vector3 rot_axis(1.,0.,0.);
rot_axis.setX(cos(azimuth_/180.*pi));
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(0);
//toggle drill and cube
if (toggle_)
{
tf::Vector3 field_orientation(1.,0.,0.);
tf::Vector3 rot_axis(0.,0.,1.);
rot_axis.setX(0);
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(cos(azimuth_/180.*pi));
}
else
{
tf::Vector3 field_orientation(0.,0.,1.);
tf::Vector3 rot_axis(1.,0.,0.);
rot_axis.setX(cos(azimuth_/180.*pi));
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(0);
}
edit: I define the toggle_ function at the beginning of the code.
namespace mag_control {
ParticleControlPanel::ParticleControlPanel(QWidget *parent) :
rviz::Panel(parent),
magfield_topic_("/desired_field"),
magfield_grad_topic_("/desired_field_grad"),
holding_lock_(false),
activated_(false),
controlling_(false),
wobble_(false),
toggle_(false),
frequency_(0.00),
azimuth_(0.0),
rot_angle_(0.0),
t_prev_(0.0),
pi(std::acos(-1)),
gradient_(0.,0.,0.),
position_(0.,0.,0.),
z_control_(false),
gradient_z_(0.0),
thresh_bin_(0),
thresh_hough_(0),
config_("demo.yaml")
{
Edit: I hear that the variables field_orientation and rot_axis are not the same inside of the if-else and below it. How can I change this? Again, this is something I have not a lot experience with, but I need to fix because of work circunstances.
void ParticleControlPanel::processCheckboxToggle(int value)
{
if(value){
toggle_ = true;
}
else {
toggle_ = false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
变量
field_orientation
和rot_axis
您在内声明或else
块完全无关与变量field_orientation 和
语句之前声明之前声明。因为它们共享名称,所以较小范围内的变量 shadow 外部范围中的名称,并且您只能从较小的范围访问变量。另外,由于这些是具有自动寿命的变量,因此当它们的范围结束时,它们会被破坏(在这种情况下 -rot_axis
您在}
关闭如果达到 block)。的简化示例:
输出为(
请参阅代码 原始
field_orientation
和rot_axis
的值,请勿在中声明新变量,如果
范围,只需参考这些变量:Variables
field_orientation
androt_axis
you declare insideif
orelse
blocks are completely unrelated to variablesfield_orientation
androt_axis
you declared beforeif
statement. Because they share names, variables inside smaller scopes shadow the names in the outer scope, and you can only access variables from smaller scope. Also, since these are variables with automatic lifetime, their are destroyed when their scope ends (in this case - when}
closingif
block is reached).See a simplified example of your code:
The output is (see it online)
If you want to change the values of original
field_orientation
androt_axis
, do not declare new variables inif
scope, simply refer to those variables: