многопоточность c ++ - попытка сделать неконстантные объекты постоянными - PullRequest
0 голосов
/ 10 апреля 2020

У меня есть программа на c ++, sfml, и при попытке реализовать многопоточность возникла проблема. Во-первых, у меня мало опыта в многопоточности. Когда я go назначаю задачи потока, он выбрасывает 22 новые ошибки, связанные с объектами const, но объекты не являются const. Спасибо. извините, если название написано плохо.

вот код для просмотра

class NewKey {
public:
    sf::Image Img;
    sf::Texture Tex;
    sf::Sprite Sprite;
    sf::Vector2i Velocity;
    sf::Vector2i Acceleration;
    sf::Vector2i NextPos;
    bool isMovingLeft() {
        if (Velocity.x < 0)
            return true;
        else
            return false;
    }
    bool isMovingRight() {
        if (Velocity.x > 0)
            return true;
        else
            return false;
    }
    void velocityCap() {
        if (Velocity.x >= 130)
            Velocity.x = 130;
        if (Velocity.y >= 130)
            Velocity.y = 130;
        if (Velocity.x <= -130)
            Velocity.x = -130;
        if (Velocity.y <= -130)
            Velocity.y = -130;
    }
    void loseSpeed(int x) {
        if (isMovingLeft()) {
            Acceleration.x += x;
        }
        else if (isMovingRight())
            Acceleration.x -= x;;
    }
    void StepVelocity() {
        Velocity += Acceleration;
        Acceleration = sf::Vector2i(0, 0);
    }
};

void foo() {
    NewKey Key;
    std::thread thread1;
    std::unique_ptr <sf::RenderWindow> window = std::make_unique<sf::RenderWindow>(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::None);
    Key.NextPos = Gravity(Key, window, thread1);
    thread1.join();
}

sf::Vector2i Gravity(NewKey& Key, std::unique_ptr <sf::RenderWindow>& window, std::thread& thread1) {
    int windSpeed = 2;
    int dropRate = 10;
    thread1 = std::thread([=] {
        if (Key.Velocity.x == 1)
            Key.loseSpeed(1);
        else
            Key.loseSpeed(windSpeed);
        Key.StepVelocity();
        Key.velocityCap();
        if (Key.Velocity.y < 10 && Key.Velocity.y > 0) {
            Key.Velocity.y = 0;
        }
        });
    sf::Vector2i Result = sf::Vector2i(window->getPosition() + Key.Velocity);
    return Result;
}

int main()
{
    foo();
}

ошибки

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E1086   the object has type qualifiers that are not compatible with the member function "NewKey::loseSpeed" Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    61  
Error (active)  E1086   the object has type qualifiers that are not compatible with the member function "NewKey::loseSpeed" Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    63  
Error (active)  E1086   the object has type qualifiers that are not compatible with the member function "NewKey::StepVelocity"  Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    64  
Error (active)  E1086   the object has type qualifiers that are not compatible with the member function "NewKey::velocityCap"   Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    65  
Error (active)  E0137   expression must be a modifiable lvalue  Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    67  
Error   C3861   'Gravity': identifier not found Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    53  
Error   C2662   'void NewKey::loseSpeed(int)': cannot convert 'this' pointer from 'const NewKey' to 'NewKey &'  Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    61  
Error   C2662   'void NewKey::loseSpeed(int)': cannot convert 'this' pointer from 'const NewKey' to 'NewKey &'  Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    63  
Error   C2662   'void NewKey::StepVelocity(void)': cannot convert 'this' pointer from 'const NewKey' to 'NewKey &'  Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    64  
Error   C2662   'void NewKey::velocityCap(void)': cannot convert 'this' pointer from 'const NewKey' to 'NewKey &'   Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    65  
Error   C3490   'y' cannot be modified because it is being accessed through a const object  Error   C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp    67  
...