小编Juj*_*nol的帖子

C++使用构造函数填充的向量的奇怪行为

我正在经历一些使用构造函数填充向量值的"奇怪行为".

所以,我有这个块类,只要调用构造函数,就会填充这个名为blockList的静态向量.

static std::vector<Block*> blockList;

Block::Block(float x, float y)
    : RectangleShape(sf::Vector2f(WIDTH, HEIGHT)) {
    blockList.push_back(this);
    setFillColor(sf::Color(221, 221, 221, 255));
    setPosition(x, y);
}
Run Code Online (Sandbox Code Playgroud)

现在,当我在另一个类中填充值时,我称之为"游戏"; 在它的构造函数中如下:

Game::Game(sf::RenderWindow& window) {
    // other code here

    Block firstBlock(10, 10);
    // possible more blocks here
}
Run Code Online (Sandbox Code Playgroud)

然后试图画画,它崩溃了.另外,该块的坐标打印为(x:9.8439e-12,y:9.84394e-12).现在我尝试做的是把它扔进draw方法(我知道它很糟糕,因为它经常被调用但是出于调试目的):

void Game::drawObjects() {
    // draw other shapes here

    Block oneBlock(50.0f, 50.0f);

    std::cout << std::string(10, '-') << std::endl;
    for (Block* block : Block::getBlockList()) {
        std::cout << block->getPosition().x << " - " << block->getPosition().y << std::endl;
        window->draw(*block);
    }
    std::cout << std::string(10, '-') …
Run Code Online (Sandbox Code Playgroud)

c++ runtime-error vector stdvector

3
推荐指数
1
解决办法
42
查看次数

C++ getline方法不起作用

对不起,我对C++很新,但不是一般的编程.所以我试着做一个简单的加密/解密.但是,当我将修改添加到我之前的代码中时(因此没有两个加密和解密程序),我发现代码'getline()'方法不再有效.相反,它只是在代码运行时忽略它.这是代码:

int main(){
    std::string string;
    int op = 1; //Either Positive or Negative

    srand(256);
    std::cout << "Enter the operation: " << std::endl;
    std::cin >> op;
    std::cout << "Enter the string: " << std::endl;
    std::getline(std::cin, string); //This is the like that's ignored

    for(int i=0; i < string.length(); i++){
        string[i] += rand()*op; //If Positive will encrypt if negative then decrypt
    }
    std::cout << string << std::endl;

    std::getchar(); //A Pause 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ encryption cin

1
推荐指数
1
解决办法
293
查看次数

标签 统计

c++ ×2

cin ×1

encryption ×1

runtime-error ×1

stdvector ×1

vector ×1