Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 修正-Werror=未使用但已设置的变量_C++_Compilation - Fatal编程技术网

C++ 修正-Werror=未使用但已设置的变量

C++ 修正-Werror=未使用但已设置的变量,c++,compilation,C++,Compilation,这可能是一个愚蠢的问题,但如果不添加打印语句,我无法解决这个问题 这是我的密码 double best_mother_index = 0; double best_mother_energy = -9999; double best_mother_plane = -99; for(size_t p=0; p< marks_mother_vector.size(); p++) { art:

这可能是一个愚蠢的问题,但如果不添加打印语句,我无法解决这个问题

这是我的密码

        double best_mother_index = 0;
        double best_mother_energy = -9999;
        double best_mother_plane = -99;

        for(size_t p=0; p< marks_mother_vector.size(); p++)
        {
           art::Ptr<simb::MCParticle> mother = marks_mother_vector[p];
           std::vector<double> mother_energy_recod = marks_mother_energy_fraction_map[mother];

           if( mother_energy_recod[0] > best_mother_energy){
                best_mother_index = p;
                best_mother_energy = mother_energy_recod[0];
                best_mother_plane = 0;
           }
           if (mother_energy_recod[1] > best_mother_energy){
                best_mother_index = p;
                best_mother_energy = mother_energy_recod[1];
                best_mother_plane = 1;
           }
           if( mother_energy_recod[2] > best_mother_energy){
                best_mother_index = p;
                best_mother_energy = mother_energy_recod[2];
                best_mother_plane = 2;
           }
        }

i get the following error message when compiling

:9: error: variable ‘best_mother_plane’ set but not used [-Werror=unused-but-set-variable]
  double best_mother_plane = -99;

double-best\u-mother\u索引=0;
双倍最佳能量=-9999;
双最佳平面=-99;
对于(size_t p=0;p最佳母能量){
最佳母亲指数=p;
最佳母亲能量=母亲能量记录[0];
最佳母面=0;
}
if(母亲能量记录[1]>最佳母亲能量){
最佳母亲指数=p;
最佳母亲能量=母亲能量记录[1];
最佳母面=1;
}
if(母亲能量记录[2]>最佳母亲能量){
最佳母亲指数=p;
最佳母亲能量=母亲能量记录[2];
最佳母面=2;
}
}
编译时,我收到以下错误消息
:9:错误:变量“最佳母平面”已设置但未使用[-Werror=未使用但已设置变量]
双最佳平面=-99;
为什么这是唯一有问题的变量?
如何解决这个问题?

这是因为您设置了变量的值,但从未以任何方式读取它-这基本上是无用的。如果您计划实现变量的使用,那么就继续吧,否则按照警告告诉您的去做,把它去掉:这是在浪费内存和性能

为什么这是唯一有问题的变量

因为这是唯一设置但未使用的变量

我怎样才能解决这个问题

要么:

  • 使用变量的值进行某些操作。考虑一下您希望该值如何影响程序的行为
  • 或者,如果您无意使用该变量,则将其完全删除
  • 从技术上讲,您可以使用[[maybe_unused]]声明变量,但是在这个简单的例子中,上面的一个选项可能会更好。当使用宏控制条件编译(用于移植到不同系统)时,它最有用
  • 当编译器注意到这样一个未使用的变量时,可以避免告诉它编译失败。上面提到的其中一个选项会更好,因为这样的变量通常表示一个错误,这使得这个警告很有价值

您根本没有使用该变量。如果不使用变量,为什么需要它?解决方案是,如果不打算使用该变量,就不要使用它。如果您计划在以后的开发中使用
最佳母平面
变量,您可以暂时禁用此警告,以检查您的程序是否可编译:
-Wno unused but set variable
,这会浪费内存和时间性能。
只要启用了优化,可能就不会。如果编译器足够聪明,可以告诉您没有使用该值,那么它应该足够聪明,不会浪费内存或性能。@eerorika true,如果启用了优化