我有一个通过引用传递变量的问题。我尝试在类的SFML窗口中绘制某些内容,但不会绘制。如果我取消注释行的注释,它将起作用。我究竟做错了什么?

#pragma once

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "ball.h"

int _tmain(int argc, _TCHAR* argv[])
{
  sf::RenderWindow window = sf::RenderWindow( sf::VideoMode( 640, 480 ), "SFML window" );

  ball my_ball = ball ( sf::Vector2f( 320.0, 240.0 ) );

  window.clear();
  my_ball.draw(window);
  //sf::CircleShape circle;
  //circle.setRadius( 30 );
  //circle.setPosition( sf::Vector2f( 320.0, 240.0 ) );
  //window.draw( circle );
  window.display();

  while(window.isOpen()) {
    sf::Event event;
    while( window.pollEvent(event) ){
      if( event.type == sf::Event::Closed ){
        window.close();
      }
    }
    sf::sleep( sf::milliseconds( 20 ) );
  }
  return 0;
}




#include <SFML/Graphics.hpp>

class ball
{
  public:
    ball( sf::Vector2f position, float size = 30.0 );

    void draw( sf::RenderWindow & window ) const;

  private:

   sf::Vector2f position;
   float size;

  };


球cpp

#include "StdAfx.h"
#include "ball.h"

ball::ball( sf::Vector2f position, float size ) {
  position = position;
  size = size;
}

void ball::draw( sf::RenderWindow & window ) const {
  sf::CircleShape circle;
  circle.setRadius( size );
  circle.setPosition( position );
  window.draw( circle );
}

最佳答案

你为什么要分配给他们自己?错了

ball::ball( sf::Vector2f position, float size ) {
  position = position;
  size = size;
}

关于c++ - C++按引用传递变量(SFML窗口),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25752347/

10-12 17:24