我收到错误消息:

error: comparison of constant 18446744073709551614 with
     expression of type 'unsigned int' is always true
     [-Werror,-Wtautological-constant-out-of-range-compare]

The line of code it is referring to is this:

if (key_index != std::string::npos)
key_index是我的unsigned int语句也位于的main()函数中的if局部变量。

我已经在这里阅读了几篇文章,试图弄清我要去哪里。

此链接是最有用的

What does string::npos mean

但是,即使利用那里的建议,我仍然会遇到相同的错误。

我尝试创建自己的变量:
const int NPOS = -1

我以几种不同的方式修改了比较运算符。

我试图将这条线变成:
if(key_index != (std::string::npos -1))

我已经尝试了其他一些小的更改,目前还无法记忆。

我忘了写下来,对于没有全部测试/ retrofit 细节,我深表歉意。

这是问题来自的完整main()代码。就我所知,可能还需要其他一些纠正。
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include <math.h>  // For pow()
#include <string>
#include <stdexcept>  // For std::runtime_error
#include <vector>
#include "GuitarString.hpp"

const sf::Uint32 quit_key = 27;       // ASCII 27 is the Escape key

const std::string keyboard = "q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' ";
const int num_keys = keyboard.length();
const int kDuration = 8;

std::vector<sf::Int16> makeSamplesFromString(GuitarString *gs) {
    std::vector<sf::Int16> samples;

    gs->pluck();
    for (int i= 0; i < kSampleRate * kDuration; i++) {
        gs->tic();
        samples.push_back(gs->sample());
    }

    return samples;
}

int main() {
    sf::Event event;
    double frequency;
    unsigned int key_index;

    std::vector < std::vector<sf::Int16> > samples(num_keys);

    std::vector<sf::SoundBuffer> buffers(num_keys);

    std::vector<sf::Sound> sounds(num_keys);

    for (int i = 0; i < num_keys; ++i) {
        frequency = 440 * pow(2, (i-24) / 12.0L);
        GuitarString gs = GuitarString(frequency);

        samples[i] = makeSamplesFromString(&gs);

        if (!buffers[i].loadFromSamples(&samples[i][0],
                                    samples[i].size(), 2, kSampleRate)) {
            throw std::runtime_error("sf::SoundBuffer: failed to load from samples.");
        }

        sounds[i].setBuffer(buffers[i]);
    }

    sf::Vector2u size_win(500, 200);
    sf::Sprite background_sprite;
    sf::Texture background_texture;
    if (background_texture.loadFromFile("keyboard.png")) {
        background_sprite.setTexture(background_texture);
        size_win = background_texture.getSize();
    }

    sf::RenderWindow window(sf::VideoMode(size_win.x, size_win.y),
                          " PS5B GuitarHero - Press Escape to Exit ");

    window.setKeyRepeatEnabled(false);

    while (window.isOpen()) {
        while (window.pollEvent(event)) {
            switch (event.type) {
                case sf::Event::Closed:
                    window.close();
                    break;
                case sf::Event::TextEntered:
                    key_index = keyboard.find(event.text.unicode);
                    if (key_index != std::string::npos) { // the line causing the issue….
                        sounds[key_index].play();
                    } else if (event.text.unicode == quit_key) {
                        window.close();
                    }
                    break;
                default:
                    break;
            }

            window.clear(sf::Color::Black);
            window.draw(background_sprite);
            window.display();
        }
    }
    return 0;
}

最佳答案

如果您使用的是64位系统,则std::string::npos几乎可以肯定是64位数字。为了确保最大的可移植性,请使用std::string指定的类型。

std::string::size_type key_index;

不管您使用的是32位还是64位系统,这都应该有效。

关于c++ - unsigned int的比较始终为真(npos问题?),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49566816/

10-09 05:58