[-Werror=unused-but-set-variable] 报错的解决

文章讨论了在C++编程中遇到的编译警告,即某些变量被设定但未在代码中使用,如变量turn和ret。这通常不会影响程序运行,但提示开发者可能有优化空间。在给定的代码示例中,变量turn在第10行和第1725行被设定,而变量ret在第33行被设定,但均未被有效使用。解决这些警告通常意味着删除未使用的变量或确保它们在适当的地方被引用。
摘要由CSDN通过智能技术生成

错误: 变量‘xxx’被设定但未被使用 [-Werror=unused-but-set-variable]

warning: variable set but unused - C++ Forum

出现这个警告的原因可能是,基本上,你移除这个变量也不会对你的程序造成什么影响。

warning: variable 'ret' set but not used [-Wunused-but-set-variable]
warning: variable 'turn' set but not used [-Wunused-but-set-variable]

receiving these errors even though I am using them

Please guide me to the solution of these warnings!

I use turn on lines: 10, 17 25

I use ret on line: 33

int OlsonAttack(int mode, int opponent) //ATTACK FUNCTION
{
    int column;
    char row;
    int ret;

    //******    STATUS VERIABLES    ******//
    static int board[BS_GRID_ROWS][BS_GRID_COLS]; // board will not change

    static bool turn; //allows turn to end || not to fire > once
    static bool hit; //fired is for shot per turn
    //***********************************//

    switch(mode) {
    case BS_MODE_NEW_GAME: //NEW GAME
     
        turn = true;

        for (row = 0; row < BS_GRID_ROWS; row++) //search/increment row
            for(column = 1; column < BS_GRID_COLS; column++) //search increment column
               board[row+'A'][column] = 0;
        break;

    case BS_MODE_CONTINUE_GAME: //CONTINUE GAME (no error)
    turn = true;
    if (!hit) {
        do {
            row = rand() % BS_GRID_ROWS;
            column = rand() % BS_GRID_COLS;
        }while (board[row+'A'][column] == 1);

        board[row+'A'][column] = 1; //Marks a probed unit of space on array
        ret = fire[opponent](row + 'A', column + 1);

    }
        break;
    }
    return 0;
}

 > I use turn on lines: 10, 17 25
line 10: static bool turn; definition, not a use
line 17 and 25:turn = true; setting, not a use

> I use ret on line: 33
ret = fire[opponent](row + 'A', column + 1); setting, not a use


Bascally, if you remove those two variables, the behaviour of your program should not change.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值