• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

c++ multithreading – trying to make non-const objects const

April 10, 2020 by

Questions › c++ multithreading – trying to make non-const objects const
0
Vote Up
Vote Down
Garmaine asked 4 years ago

I have a c++, sfml program and upon trying to implement multi threading have ran into a problem. First off I have little experience with multi threading. When I go to assign the thread tasks it is throwing 22 new errors all relating to objects being const but the objects are not const. Thanks. sorry if the title is poorly worded.

here's some code to look over

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();
}

the errors

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  
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++, c++17, multithreading, sfml

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023