gpt4 book ai didi

c++ - unsigned int 的比较总是正确的(npos 问题?)

转载 作者:太空狗 更新时间:2023-10-29 20:01:56 27 4
gpt4 key购买 nike

我收到一个错误:

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 是我的 main() 函数中的一个 unsigned int 局部变量,我的 if 语句也是中。

我已经通读了这里的一些帖子,试图弄清楚我哪里出了问题。

这个链接是最有用的

What does string::npos mean

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

我尝试创建自己的变量:

const int NPOS = -1

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

我试着把这条线变成:

if(key_index != (std::string::npos -1))

我尝试了一些其他的小改动,目前我想不起来了。

我忘了把它们写下来,很抱歉没有我所有的测试/修改细节。

这是问题来源的完整 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com