2016-07-13 3 views
-1

이 문제를 3 시간 동안 해결하려고 노력했지만 그럴 수 없습니다. 나는 C++를 배우려고 노력하고 있지만 여기에 붙어 있습니다. 나는 그 문제를 모른다. 내 총알 개체와 함께 사용할 개체 풀을 만들려고했기 때문에 메모리 누출없이 쉽게 생성 및 생성 할 수있었습니다. 다음과 같이 내 소스는 다음과 같습니다기본 정의가 잘못 작성되어 SFML C++ 공용 생성자가 암시 적으로 삭제되었습니다.

MAIN.CPP

고마웠다 첫 번째 오류는 총알 클래스의 생성자와 BulletPool 클래스의 업데이트 방법입니다.

#include <SFML/Graphics.hpp> 
#include <stdio.h> 
#include <time.h> 
#include <assert.h> 

#include "bullets.hpp" 
#include "enemies.hpp" 

BulletPool::BulletPool() { //no matching function for call to 'Bullet::Bullet()' 
    // The first one is available. 
    firstAvailable_ = &bullets_[0]; 

    // Each particle points to the next. 
    for (int i = 0; i < POOL_SIZE - 1; i++) 
    { 
    bullets_[i].setNext(&bullets_[i + 1]); 
    } 

    // The last one terminates the list. 
    bullets_[POOL_SIZE - 1].setNext(NULL); 
} 


Bullet::Bullet(sf::Vector2f pos, sf::Color color, float bullet_radius, bool enemy, sf::Vector2f vel) { // candidate expects 6 arguments, 0 provided 
    onScreen_=true; 
    inUse = true; 
    state_.live.enemy_ = enemy; 
    state_.live.velocity_ = vel; 
    shape.setRadius(bullet_radius); 
    shape.setOrigin(bullet_radius, bullet_radius); 
    shape.setFillColor(color); 
    shape.setPosition(pos); 
} 

void Bullet::update(sf::Vector2f screen) { 
    shape.move(state_.live.velocity_); 
    if((shape.getGlobalBounds().top < 0 || shape.getGlobalBounds().top > 200/*screen.y*/)&& !state_.live.enemy_) { 
     onScreen_ = false; 
    } 

    if(!onScreen_) { 
     inUse = false; 
    } 
} 
void BulletPool::create(sf::Vector2f pos, sf::Color color, float bullet_radius, bool enemy, sf::Vector2f vel) { 

    assert(firstAvailable_ != NULL); 

    Bullet* newBullet = firstAvailable_; 
    firstAvailable_ = newBullet->getNext(); 
    newBullet = new Bullet(pos,color,bullet_radius,enemy,vel); 

    // Find an available particle. 
    /*for (int i = 0; i < POOL_SIZE; i++) { 

     if (!bullets_[i].inUse()) { 

      bullets_[i].init(pos,color,bullet_radius,enemy,vel); 
      return; 
     } 
    }*/ 
} 

void BulletPool::update(sf::RenderWindow window) { //initializing argument 1 of 'void BulletPool::update(sf::RenderWindow)' 
    for (int i = 0; i < POOL_SIZE; i++) { 
     if (!bullets_[i].inUse) { 
      // Add this particle to the front of the list. 
      bullets_[i].setNext(firstAvailable_); 
      firstAvailable_ = &bullets_[i]; 
     } else { 
      window.draw(bullets_[i].shape); 
     } 
    } 
} 
Enemy::~Enemy() {}; 
RedShip::RedShip(sf::Vector2f pos,float side_ln) { 
    movement = 10; 
    shape.setPointCount(6); 
    shape.setPoint(0, sf::Vector2f(side_ln/2, 0)); 
    shape.setPoint(1, sf::Vector2f(side_ln*3/8,side_ln*3/8)); 
    shape.setPoint(2, sf::Vector2f(0,side_ln*1/8)); 
    shape.setPoint(3, sf::Vector2f(side_ln/2,side_ln)); 
    shape.setPoint(4, sf::Vector2f(side_ln,side_ln*1/8)); 
    shape.setPoint(5, sf::Vector2f(side_ln*5/8,side_ln*3/8)); 
    shape.setFillColor(sf::Color::Transparent); 
    shape.setOutlineColor(sf::Color::Red); 
    shape.setOutlineThickness(5); 
    shape.setOrigin(side_ln/2, sqrt((3*side_ln*side_ln)/4)); 
    shape.setPosition(pos); 
} 

void RedShip::update(float base_time, BulletPool bullets) { 
    if(movement > 1) { 
     shape.move(3,1.5); 
     movement--; 
    } 
    else if(movement < -1) { 
     shape.move(-3,1.5); 
     movement++; 
    } 
    else if(movement == -1) 
     movement = 30; 
    else 
     movement = -30; 

    if(fmod(base_time,20)==0) { 
    bullets.create({shape.getPosition().x, shape.getPosition().y + shape.getLocalBounds().height/2}, sf::Color::Red, 4,true, {0,6}); 
    } 
} 


GreenShip::GreenShip(sf::Vector2f pos, float side_ln) { 
    movement={}; 
    shape.setPointCount(8); 
    shape.setPoint(0, sf::Vector2f(0, 0)); 
    shape.setPoint(1, sf::Vector2f(0,side_ln*2/3)); 
    shape.setPoint(2, sf::Vector2f(side_ln/2,side_ln)); 
    shape.setPoint(3, sf::Vector2f(side_ln,side_ln*2/3)); 
    shape.setPoint(4, sf::Vector2f(side_ln,0)); 
    shape.setPoint(5, sf::Vector2f(side_ln*3/4,side_ln/2)); 
    shape.setPoint(6, sf::Vector2f(side_ln/2,side_ln*3/8)); 
    shape.setPoint(7, sf::Vector2f(side_ln*1/4,side_ln/2)); 
    shape.setFillColor(sf::Color::Transparent); 
    shape.setOutlineColor(sf::Color::Green); 
    shape.setOutlineThickness(5); 
    shape.setOrigin(side_ln/2, sqrt((3*side_ln*side_ln)/4)); 
    shape.setPosition(pos); 
} 

void GreenShip::update(float base_time,BulletPool bullets) { 

    shape.move(0,1); 

    if(fmod(base_time,60)==0) { 
    //bullets.push_back(new Bullet({shape.getPosition().x, shape.getPosition().y + shape.getLocalBounds().height/2 + 4}, sf::Color::Green, 4,true, 2)); 
    } 
} 

class Core { 
public: 
    const float core_velocity{5}; 
    int bullet_count{0}; 

    sf::ConvexShape shape; 
    sf::Vector2f velocity; 

    Core(float sX, float sY, float scale) { 
     shape.setPointCount(12); 
     shape.setPoint(0,{scale/2,0}); 
     shape.setPoint(1,{scale*7/16,scale*2/16}); 
     shape.setPoint(2,{scale*6/16,scale*4/16}); 
     shape.setPoint(3,{scale*5/16,scale*7/16+scale/3}); 
     shape.setPoint(4,{0,scale*10/16+scale/2}); 
     shape.setPoint(5,{0,scale*13/16+scale/3}); 
     shape.setPoint(6,{scale*8/16,scale*10/16+scale/3}); 
     shape.setPoint(7,{scale,scale*13/16+scale/3}); 
     shape.setPoint(8,{scale,scale*10/16+scale/2}); 
     shape.setPoint(9,{scale*11/16,scale*7/16+scale/3}); 
     shape.setPoint(10,{scale*10/16,scale*4/16}); 
     shape.setPoint(11,{scale*9/16,scale*2/16}); 
     shape.setFillColor(sf::Color::Transparent); 
     shape.setOutlineColor(sf::Color::White); 
     shape.setOutlineThickness(7); 
     shape.setOrigin(scale/2, scale/2); 
     shape.setPosition(sX, sY); 
    } 

    void update() { 

     if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) 
      velocity.x = -core_velocity; 
     else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) 
      velocity.x = core_velocity; 
     else 
      velocity.x = 0; 

     if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) 
      velocity.y = -core_velocity; 
     else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) 
      velocity.y = core_velocity; 
     else 
      velocity.y = 0; 

     shape.move(velocity); 

    } 

    void fire(BulletPool bullets) { 
     if(bullet_count < 3) { 
      bullets.create({shape.getPosition().x, shape.getPosition().y-shape.getLocalBounds().height/2 -1}, sf::Color::White, 6,false,{0,-8}); 
      bullet_count++; 
     } 
    } 
}; 


int main(int argc, char *argv[]) { 

    const sf::Vector2f res{480,640}; 
    printf("Religion ist das Opium des Volkes.\n    -Marx\n"); 
    sf::RenderWindow window(sf::VideoMode(res.x,res.y), "Brick Breaker", sf::Style::None); 

    float base_time{0}; 
    Core core(res.x/2, res.y /2 + 200, 64); 
    GreenShip ship({res.x/2,50},50); 
    GreenShip ship1({res.x/2+100,50},50); 
    BulletPool bullets; 
    //std::vector<Enemy*> enemies; 


    while (window.isOpen()) 
    { 
     sf::Event event; 
     while (window.pollEvent(event)) 
     { 
      switch (event.type) 
       { 

        case sf::Event::Closed: 
         window.close(); 
         break; 

        case sf::Event::KeyPressed: 
         switch (event.key.code) { 

          case sf::Keyboard::Q: 
           core.fire(bullets); 
           break; 

          case sf::Keyboard::Escape: 
           window.close(); 
           break; 

          default: 
           break; 
         } 
         break; 

        default: 
         break; 
     } 
     } 

     if(base_time<3600) 
      base_time++; 
     else 
      base_time=0; 

     window.clear(); 
     window.setFramerateLimit(60); 

     core.update(); 
     window.draw(core.shape); 

     ship.update(base_time,bullets); 
     window.draw(ship.shape); 

     ship1.update(base_time,bullets); 
     window.draw(ship1.shape); 

     bullets.update(window); //use of deleted function 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)' 

     /*for(auto& b : bullets) { 
      if(!b->onScreen) { 
       core.bullet_count--; 
      } 
      else { 
       window.draw(b->shape); 
       b->update(res,base_time,bullets); 
      } 
     }*/ 
     window.display(); 
    } 

    return 0; 
} 

enemies.hpp (이 하나에 오류)

#ifndef ENEMIES_HPP 
#define ENEMIES_HPP 

class Enemy{ 
public: 
    sf::ConvexShape shape; 
    int movement; 
    virtual void update(float base_time, BulletPool bullets) = 0; 
    virtual ~Enemy() = 0; 
}; 



class RedShip: public Enemy{ 
public: 
    RedShip(sf::Vector2f pos, float side_ln); 
    void update(float base_time, BulletPool bullets); 
}; 

class GreenShip: public Enemy{ 
public: 
    GreenShip(sf::Vector2f pos, float side_ln); 
    void update(float base_time, BulletPool bullets); 
}; 



#endif 

bullets.hpp는

#ifndef BULLETS_HPP 
#define BULLETS_HPP 

class Bullet{ //Bullet::Bullet(Bullet&&)// CANT UNDERSTAND WHY THIS POPS UP 
public: 
    sf::CircleShape shape; 
    Bullet(); //unless i add this constructor it gives the error below↓ 
    Bullet* getNext() const { return state_.next_; } 
    void setNext(Bullet* next) { state_.next_ = next; } 
    Bullet(sf::Vector2f pos, sf::Color color, float bullet_radius, bool enemy, sf::Vector2f vel); //Bullet::Bullet(sf::Vector2f, sf::Color, float, bool, float, float) 
    void update(sf::Vector2f screen); 
    bool inUse; 

private: 
    bool onScreen_=true; 
    union { //'Bullet::<anonymous union>::<constructor>()' is implicitly deleted because the default definition would be ill-formed: 
     struct { 
     bool enemy_; 
     sf::Vector2f velocity_; 
     } live; //union member 'Bullet::<anonymous union>::live' with non-trivial 'Bullet::<anonymous union>::<anonymous struct>::<constructor>()' 

     Bullet *next_; 

    } state_; 
}; 

class BulletPool{ 
public: 
    BulletPool(); 
    void create(sf::Vector2f pos, sf::Color color, float bullet_radius, bool enemy, sf::Vector2f vel); 
    void update(sf::RenderWindow window); 

private: 
    static const int POOL_SIZE = 100; 
    Bullet bullets_[POOL_SIZE]; 
    Bullet* firstAvailable_; 
}; 

#endif 

모든 오류를 그들은 일식에 표시됩니다 같이

[solved]use of deleted function 'Bullet::<anonymous union>::<constructor>()' 

initializing argument 1 of 'void BulletPool::update(sf::RenderWindow)' 

use of deleted function 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)' 

'Bullet::<anonymous union>::<constructor>()' is implicitly deleted because the default definition would be ill-formed: 

union member 'Bullet::<anonymous union>::live' with non-trivial 'Bullet::<anonymous union>::<anonymous struct>::<constructor>()' 

컴파일러 로그 (mingw g ++) :

10:22:57 **** Incremental Build of configuration Debug for project Black Lambda **** 
Info: Configuration "Debug" uses tool-chain "MinGW GCC" that is unsupported on this system, attempting to build anyway. 
Info: Internal Builder is used for build 
g++ -std=c++0x -DSFML_STATIC -D__GX_EXPERIMENTAL_CXX_0X__ -D_cplusplus=201103L "-IC:\\SFML-2.3.2\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\\main.cpp" 
..\main.cpp: In constructor 'Bullet::Bullet(sf::Vector2f, sf::Color, float, bool, sf::Vector2f)': 
..\main.cpp:24:100: error: use of deleted function 'Bullet::<anonymous union>::<constructor>()' 
Bullet::Bullet(sf::Vector2f pos, sf::Color color, float bullet_radius, bool enemy, sf::Vector2f vel) { 
                            ^
In file included from ..\main.cpp:6:0: 
..\bullets.hpp:16:8: note: 'Bullet::<anonymous union>::<constructor>()' is implicitly deleted because the default definition would be ill-formed: 
    union { 
     ^
..\bullets.hpp:20:5: error: union member 'Bullet::<anonymous union>::live' with non-trivial 'Bullet::<anonymous union>::<anonymous struct>::<constructor>()' 
    } live; 
    ^
..\main.cpp: In function 'int main(int, char**)': 
..\main.cpp:261:30: error: use of deleted function 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)' 
     bullets.update(window); 
          ^
In file included from C:\SFML-2.3.2\include/SFML/Graphics.hpp:47:0, 
       from ..\main.cpp:1: 
C:\SFML-2.3.2\include/SFML/Graphics/RenderWindow.hpp:44:25: note: 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)' is implicitly deleted because the default definition would be ill-formed: 
class SFML_GRAPHICS_API RenderWindow : public Window, public RenderTarget 
         ^
C:\SFML-2.3.2\include/SFML/Graphics/RenderWindow.hpp:44:25: error: use of deleted function 'sf::Window::Window(const sf::Window&)' 
In file included from C:\SFML-2.3.2\include/SFML/Window.hpp:42:0, 
       from C:\SFML-2.3.2\include/SFML/Graphics.hpp:32, 
       from ..\main.cpp:1: 
C:\SFML-2.3.2\include/SFML/Window/Window.hpp:57:23: note: 'sf::Window::Window(const sf::Window&)' is implicitly deleted because the default definition would be ill-formed: 
class SFML_WINDOW_API Window : GlResource, NonCopyable 
        ^
In file included from C:\SFML-2.3.2\include/SFML/System/FileInputStream.hpp:34:0, 
       from C:\SFML-2.3.2\include/SFML/System.hpp:35, 
       from C:\SFML-2.3.2\include/SFML/Window.hpp:32, 
       from C:\SFML-2.3.2\include/SFML/Graphics.hpp:32, 
       from ..\main.cpp:1: 
C:\SFML-2.3.2\include/SFML/System/NonCopyable.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private 
    NonCopyable(const NonCopyable&); 
    ^
In file included from C:\SFML-2.3.2\include/SFML/Window.hpp:42:0, 
       from C:\SFML-2.3.2\include/SFML/Graphics.hpp:32, 
       from ..\main.cpp:1: 
C:\SFML-2.3.2\include/SFML/Window/Window.hpp:57:23: error: within this context 
class SFML_WINDOW_API Window : GlResource, NonCopyable 
        ^
In file included from C:\SFML-2.3.2\include/SFML/Graphics.hpp:47:0, 
       from ..\main.cpp:1: 
C:\SFML-2.3.2\include/SFML/Graphics/RenderWindow.hpp:44:25: error: use of deleted function 'sf::RenderTarget::RenderTarget(const sf::RenderTarget&)' 
class SFML_GRAPHICS_API RenderWindow : public Window, public RenderTarget 
         ^
In file included from C:\SFML-2.3.2\include/SFML/Graphics.hpp:45:0, 
       from ..\main.cpp:1: 
C:\SFML-2.3.2\include/SFML/Graphics/RenderTarget.hpp:51:25: note: 'sf::RenderTarget::RenderTarget(const sf::RenderTarget&)' is implicitly deleted because the default definition would be ill-formed: 
class SFML_GRAPHICS_API RenderTarget : NonCopyable 
         ^
In file included from C:\SFML-2.3.2\include/SFML/System/FileInputStream.hpp:34:0, 
       from C:\SFML-2.3.2\include/SFML/System.hpp:35, 
       from C:\SFML-2.3.2\include/SFML/Window.hpp:32, 
       from C:\SFML-2.3.2\include/SFML/Graphics.hpp:32, 
       from ..\main.cpp:1: 
C:\SFML-2.3.2\include/SFML/System/NonCopyable.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private 
    NonCopyable(const NonCopyable&); 
    ^
In file included from C:\SFML-2.3.2\include/SFML/Graphics.hpp:45:0, 
       from ..\main.cpp:1: 
C:\SFML-2.3.2\include/SFML/Graphics/RenderTarget.hpp:51:25: error: within this context 
class SFML_GRAPHICS_API RenderTarget : NonCopyable 
         ^
..\main.cpp:64:6: error: initializing argument 1 of 'void BulletPool::update(sf::RenderWindow)' 
void BulletPool::update(sf::RenderWindow window) { 
    ^

10:22:59 Build Finished (took 1s.427ms) 

난 그냥 C++ 그렇게 어떤 도움이나 critism 감사합니다, 감사를 배우려고 노력하고 있어요!

편집 모든 도움에 대해 감사드립니다. 나는 모든 문제를 밖으로 생각했다. BulletPool :: update의 문제점은 renderwindow 객체가 참조없이 전달 되었기 때문에 함수가 창을 복사하여 렌더링하지 못하게했습니다.

이 오류 메시지가 말하는대로, 당신이 union 회원을 가지고 있기 때문에 발생
+3

'sf :: Vector2f'는 * pod *가 아니므로 'union'은 [Unrestricted_union] (https://en.wikipedia.org/wiki/C%2B%2B11#Unrestricted_unions)입니다. – Jarod42

+0

@RichardDally 내가 사용하고있을 때 글 머리 기호 개체의 값을 저장할 때 다음 포인터의 값을 저장할 수 있도록 유니온을 설정하려고합니다. –

+0

@ Jarod42 감사합니다. 포드가있는 것을 잘 모르는 사람이 많습니다. –

답변

0

(익명 struct) 기본 생성자없이 구성원 (velocity_)가 그 (또는 비 사소한 일,과) 전체한다 (일반 오래된 데이터 형식이 아닌) POD가 아닌 구조체를 구성하면 컴파일러를 막을 수 있습니다.

간단히 말하면 관련 항목은 union에 있거나 (이 경우에는 sf::Vector2f) 그리고 이 포함 된 구조체 (예 : 포함 된 구조체 또는 클래스)는의 기본 생성자를 작성해야합니다. 예를 들어. 소멸자, 사본 등)

그리고 그것에 대해 생각하면 합리적입니다. 당신이 당신의 노동 조합에 복잡한 것을 가지고 있다면, 컴파일러는 그 노동 조합의 인스턴스가 그들의 삶을 시작하는 상태를 알 필요가 있습니다.

기본적으로 솔루션은 공용체 형식을 명명 한 다음 간단한 (심지어 비어있는) 기본 생성자를 작성합니다. next_ 포인터를 nullptr 또는 this+1 또는 무엇인가로 초기화하고 싶을 수도 있습니다. 다른 기본 방법을 작성하거나 최소한 지정해야 할 수도 있습니다.

+0

그럼에도 불구하고 main.cpp의 오류는 여전히 존재하지만 통합 문제는 해결되었습니다. 왜 이해가 안되니? 나는 sf :: Vector2f 대신에 2 개의 float을 필요로하므로 소스를 편집했지만 여전히 오류가 있습니까? 또한 빈 생성자를 먼저 추가하지 않으면 bullets.hpp 파일에서 주 생성자가 오류를 발생시킵니다. –

+0

괜찮아. 알아 냈어. 문제는 아마 총알 객체가 기본 생성자에 할당되고있는 클래스의 구조체가 beacuse 였기 때문에 차례대로 내 생성자가 작동하지 않게되었습니다. –