C++ 警告:变量已设置但未使用 T offset=ps->first,prev\u offset; bool first=true; 而(1){ 如果(第一) 第一个=假; 其他的 断言(偏移量>上一偏移量); pl=l.find_inc(抵销); 上一个偏移量=偏移量; 如果(pl==l.m.end()) 打破 而(ps!=s.m.end()&&ps->first+ps->second first) ++聚苯乙烯; 如果(ps==s.m.end()) 打破 偏移量=pl->第一个+pl->第二个; 如果(先偏移){ 偏移量=ps->first; 继续; } }

C++ 警告:变量已设置但未使用 T offset=ps->first,prev\u offset; bool first=true; 而(1){ 如果(第一) 第一个=假; 其他的 断言(偏移量>上一偏移量); pl=l.find_inc(抵销); 上一个偏移量=偏移量; 如果(pl==l.m.end()) 打破 而(ps!=s.m.end()&&ps->first+ps->second first) ++聚苯乙烯; 如果(ps==s.m.end()) 打破 偏移量=pl->第一个+pl->第二个; 如果(先偏移){ 偏移量=ps->first; 继续; } },c++,C++,//我得到了“prev_offset”的[-Wunused but set variable]警告,除了添加cout有几种方法可以解决这个问题之外,还有其他更好的方法吗。一种是放入一个将变量强制转换为(void)的伪语句 另一种方法是有条件地包含变量,类似于assert()根据是否设置了NDEBUG宏有条件地包含其检查 (void) prev_offset; // this should stop the warning. //使用ASSERT\u code()时,代码仅出现在调试版本中 //

//我得到了“prev_offset”的[-Wunused but set variable]警告,除了添加
cout有几种方法可以解决这个问题之外,还有其他更好的方法吗。一种是放入一个将变量强制转换为(void)的伪语句

另一种方法是有条件地包含变量,类似于
assert()
根据是否设置了
NDEBUG
宏有条件地包含其检查

(void) prev_offset; // this should stop the warning.
//使用ASSERT\u code()时,代码仅出现在调试版本中
//与assert()检查相同。该代码将在中删除
//发布版本。
#ifndef NDEBUG
#定义断言代码(代码)代码
#否则
#定义断言代码(代码)
#恩迪夫
// ...
T offset=ps->first;
断言代码(T上一个偏移量);//给它自己的路线
bool first=true;
而(1){
如果(第一)
第一个=假;
其他的
断言(偏移量>上一偏移量);
pl=l.find_inc(抵销);
断言代码(上一个偏移量=偏移量);
如果(pl==l.m.end())
打破
而(ps!=s.m.end()&&ps->first+ps->second first)
++聚苯乙烯;
如果(ps==s.m.end())
打破
偏移量=pl->第一个+pl->第二个;
如果(先偏移){
偏移量=ps->first;
继续;
}
}

上一次偏移的数据类型是什么?如果您查看第一行,它没有初始化,但您在这里使用它:assert(offset>prev_offset);把它拿走。阅读
assert
。可能重复@Asseh的第一次循环迭代,它没有被使用。每次后续循环迭代,它都会被上一次迭代指定一个值。
(void) prev_offset; // this should stop the warning.
// When you use ASSERT_CODE() the code only appears in debug builds
// the same as the assert() checks. That code is removed in 
// release builds.

#ifndef NDEBUG
#define ASSERT_CODE(code) code
#else
#define ASSERT_CODE(code)
#endif

// ...


T offset = ps->first;
ASSERT_CODE(T prev_offset); // give it its own line
bool first = true;

while (1) {
  if (first)
    first = false;
  else
    assert(offset > prev_offset);
  pl = l.find_inc(offset);

  ASSERT_CODE(prev_offset = offset); 

  if (pl == l.m.end())
    break;
  while (ps != s.m.end() && ps->first + ps->second <= pl->first)
    ++ps;
  if (ps == s.m.end())
    break;
  offset = pl->first + pl->second;
  if (offset <= ps->first) {
    offset = ps->first;
    continue;
  }
}