[英]Why isn´t my If-Else statement working properly?
我有一個項目,我必須根據我想要的磁場發生器的旋轉場來更改一些參數。 我不是開發人員,c++ 也不是我的專業程序,但我需要找到一種使用切換功能在兩種不同配置之間進行切換的方法。 我嘗試使用 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;
}
}
您在if
或else
塊中聲明的變量field_orientation
和rot_axis
與您在if
語句之前聲明的變量field_orientation
和rot_axis
完全無關。 因為它們共享名稱,較小范圍內的變量會隱藏外部范圍內的名稱,並且您只能訪問較小范圍內的變量。 此外,由於這些是具有自動生命周期的變量,因此它們會在其作用域結束時被銷毀(在這種情況下 - if
}
到達塊時關閉)。
查看代碼的簡化示例:
#include <iostream>
int main(void)
{
int myVar = 5;
std::cout << "myVar before if: " << myVar << '\n';
if (true)
{
int myVar = 13; // a new myVar, completely different than the first myVar
std::cout << "myVar inside if: " << myVar << '\n';
} // myVar from inside if is gone here
std::cout << "myVar after if: " << myVar << '\n';
}
輸出是(在線查看)
myVar before if: 5
myVar inside if: 13
myVar after if: 5
如果要更改原始field_orientation
和rot_axis
的值,請不要在if
范圍內聲明新變量,只需引用這些變量即可:
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_)
{
field_orientation.setX(1.); // I guess, I don't know the library you are using
field_orientation.setY(0.);
field_orientation.setZ(0.);
rot_axis.setX(0);
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(cos(azimuth_/180.*pi));
}
else
{
field_orientation.setX(0.);
field_orientation.setY(0.);
field_orientation.setZ(1.);
rot_axis.setX(cos(azimuth_/180.*pi));
rot_axis.setY(sin(azimuth_/180.*pi));
rot_axis.setZ(0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.