简体   繁体   English

使用C ++的Xcode系统清除和“未使用表达式结果”错误

[英]Xcode system clear and “expression result unused” error using C++

While attempting to code a Tic Tac Toe game in Xcode to practice C++, I'm running into all sorts of issues which I have mostly fixed but I'm still having trouble understanding why I'm getting the following errors and how to properly fix them/avoid them. 尝试用Xcode编写Tic Tac Toe游戏来练习C ++时,我遇到了各种各样的问题,这些问题我大多已经解决了,但是我仍然很难理解为什么会出现以下错误以及如何正确修复他们/避免他们。

Problem 1: 问题一:

I've linked the curses library but I still get this error: conversion from string literal to 'char*' is deprecated . 我已经链接了curses库,但是仍然出现此错误:不建议从字符串文字转换为'char *'

#include <unistd.h>
#include <term.h>
void ClearScreen()
{
if (!cur_term)
{
    int result;
    setupterm( NULL, STDOUT_FILENO, &result );
    if (result <= 0) return;
}
putp( tigetstr( "clear" ) );
}  

Problem 2: I keep getting an Unused Entity Issue ( Expression result unused ). 问题2:我一直收到未使用的实体问题表达式结果未使用 )。
(gridBox is a 2-d array of type char and player is type char as well, both global variables.) (gridBox是char类型的2维数组,player也是char类型,都是全局变量。)

    if (gridBox[0,0] == gridBox[1,0] && gridBox[1,0] == gridBox[2,0]) {

    if (player == 'X') {
        return 'X';
    }
    if (player == 'O') {
        return 'O';
    }
}

Problem 3: When I try changing the if statement in the last problem to... 问题3:当我尝试将上一个问题中的if语句更改为...

    if (gridBox[0,0] == 'X' && gridBox[1,0] == 'X' && gridBox[2,0] == 'X') {
    return 'X';
}

I get this error: Comparison between pointer and integer ('char*' and 'int') . 我得到这个错误: 指针和整数('char *'和'int')之间的比较

This is driving me completely insane, please help. 这让我完全发疯,请帮忙。

Thank you all in advance! 谢谢大家!

  1. Is just that: implicit conversion from a string literal, such as "clear" , to a pointer to non-const char, char* , used to be allowed for historical reasons but has been deprecated because it's very unsafe. 就是这样:从字符串文字(例如"clear"隐式转换为指向非const char char*的指针,出于历史原因,过去曾经允许这样做,但由于不安全而被弃用。
    This can cause some issues when interfacing with C libraries or old code, but is usually easily worked around. 与C库或旧代码交互时,这可能会导致一些问题,但通常很容易解决。

    Replace it with a non-const array: 将其替换为非const数组:

    char clear[] = "clear"; putp(tigetstr(clear));

  2. Is caused by indexing the wrong way; 是由错误的索引方式引起的; a 2-D array is an array of arrays, and it is indexed like this: 一个二维数组是一个数组数组,它​​的索引如下:

    gridBox[0][0]

    The comma operator is used in some contexts if you want to evaluate two expressions in sequence and throw away the result of the first - a, b returns the value of b , so gridBox[0,0] is equivalent to gridBox[0] . 逗号操作符是在某些情况下使用,如果您希望按顺序来评价两个表达式,并丢掉第一的结果- a, b返回的值b ,所以gridBox[0,0]相当于gridBox[0] It's quite rare in practice - so rare that compilers have started issuing warnings because it's almost never what you intend. 在实践中,这种情况非常罕见-很少有编译器开始发出警告,因为这几乎从来都不是您想要的。

  3. Since gridBox is an array of arrays of char , and since because of point 2) gridBox[0,0] is equivalent to gridBox[0] , gridBox[0] is an array of char. 由于gridBoxchar数组的数组,并且由于第2点), gridBox[0,0]等效于gridBox[0]gridBox[0]是char数组。
    You can't compare arrays with == . 您不能使用==比较数组。 Instead, they are implicitly converted to pointers to the array's first element, which are then used for comparison. 而是将它们隐式转换为指向数组第一个元素的指针,然后将其用于比较。 Thus, the left hand side is a char* , and the right hand side is promoted to an int for comparison purposes, and those two can't be compared. 因此,左手边是char* ,而右手边被提升为int以便进行比较,而这两个对象无法进行比较。
    If you fix your indexing problem, this problem will disappear at the same time. 如果您解决了索引编制问题,此问题将同时消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM