I’m receiving an error:
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
is an unsigned int
local variable in my main()
function that my if
statement is also in.
I read through a few posts on here already, trying to make sense of where I’m going wrong.
This link was the most informative
What does string::npos mean
But, even utilizing the advice from there, I’m still coming up with the same errors.
I’ve tried to create my own variable:
const int NPOS = -1
I’ve modified the comparison operator a few different ways.
I’ve tried turning the line into:
if(key_index != (std::string::npos -1))
I’ve tried a few other minor changes, which I cannot recall at the moment.
I forgot to write them down, and I apologize for not having all of my test/mod details.
This is the full main()
code the issue is coming from. For all I know, there might be something else that I need to correct instead.
#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;
}
If you're on a 64bit system, std::string::npos
is almost certainly a 64bit number. To ensure maximum portability, use the type specified by std::string
.
std::string::size_type key_index;
That should work, regardless of whether you're on a 32 or 64 bit system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With