简体   繁体   English

错误C2280:尝试引用已删除的功能

[英]error C2280: attempting to reference a deleted function

I'm new to game development and very new to c++, but I've started developing a little Arkanoid game. 我是游戏开发的新手,也是c ++的新手,但我已经开始开发一款小型的Arkanoid游戏了。 I've had it running previously, but after refactoring (introducing the ArkanoidGame class) it doesnt compile and I cannot figure out why. 我以前运行它,但在重构(引入ArkanoidGame类)后它没有编译,我无法弄清楚为什么。

The error I'm getting is: 我得到的错误是:

d:\dropbox\development\gamedev\c++\arkanoid\arkanoid\main.cpp(14): error C2280:
    'ArkanoidGame::ArkanoidGame(void)' : attempting to reference a deleted function
d:\dropbox\development\gamedev\c++\arkanoid\arkanoid\arkanoidgame.h(25) : 
    compiler has generated 'ArkanoidGame::ArkanoidGame' here

I simply dont understand what this means and have no idea what to do to fix it. 我根本不明白这意味着什么,不知道如何解决它。

I've included the classes in question: 我已经包含了有问题的课程:

Main.cpp: Main.cpp的:

#include "ArkanoidGame.h"

int main() {
    ArkanoidGame game;
    game.init(800, 600);
    while (game.isRunning()) {
        game.checkInput();
        game.checkCollisions();
        game.draw();
    }
    return 0;
}

Arkanoid.h: Arkanoid.h:

#include "Ball.h"
#include "Pad.h"
#include <SFML/Graphics.hpp>
#include <stdarg.h>
#include <memory>

class ArkanoidGame
{
private:
    bool running;
public:
    void ArkanoidGame::init(int, int);
    bool ArkanoidGame::isRunning();
    void ArkanoidGame::checkCollisions();
    void ArkanoidGame::checkInput();
    void ArkanoidGame::update();
    void ArkanoidGame::draw();
    sf::RenderWindow* window;
    Pad pad;
    Ball ball;
};

ArkanoidGame.cpp: ArkanoidGame.cpp:

#include "ArkanoidGame.h"

void ArkanoidGame::init(int windowWidth, int windowHeight) {
    window = new sf::RenderWindow(sf::VideoMode(windowWidth, windowHeight), "Arkanoid!");
    window->setFramerateLimit(60);

    ArkanoidGame::running = true;

    //Init pad
    pad = Pad((float)(windowWidth / 2), (float)(windowHeight - 50));

    //Init ball
    ball = Ball(0.f, 0.f);
}

template<class T1, class T2> bool intersect(T1& mA, T2& mB) {
    return mA.right() >= mB.left() && mA.left() <= mB.right()
        && mA.bottom() >= mB.top() && mA.top() <= mB.bottom();
}

void ArkanoidGame::checkCollisions() {
    if (!intersect(pad, ball)) return;

    ball.velocity.y = -ball.ballVelocity;

    if (ball.x() < pad.x()) {
        ball.velocity.x = -ball.ballVelocity;
    }
    else {
        ball.velocity.x = ball.ballVelocity;
    }
}

void ArkanoidGame::update() {
    //Update positions
    pad.update(window->getSize().x);
    ball.update(window->getSize().x, window->getSize().y);
}

void ArkanoidGame::draw() {
    window->clear(Color::Black);
    window->draw(pad.getShape());
    window->draw(ball.getShape());
    window->display();
}

void ArkanoidGame::checkInput() {
    if (Keyboard::isKeyPressed(Keyboard::Key::Escape)) {
        running = false;
    }
}

bool ArkanoidGame::isRunning() {
    return running;
}

Presumably, either Pad or Ball (or both) has no default constructor; 据推测, PadBall (或两者)都没有默认构造函数; therefore one can't be generated for a class that contains them. 因此,不能为包含它们的类生成一个。 They must be initialised using one of their declared constructors. 必须使用其声明的构造函数初始化它们。

The best solution is to remove your weird init function, and replace it with a constructor: 最好的解决方案是删除你的奇怪的init函数,并用构造函数替换它:

ArkanoidGame(int windowWidth, int windowHeight) :
    running(true),
    window(new ...),
    Pad(windowWidth / 2, windowHeight - 50),
    Ball(0,0)
{
    window->setFramerateLimit(60);
}

int main() {
    ArkanoidGame game(800, 600);
    // ...
}

If you really want a two-stage initialisation dance for some reason, then you'll need to provide default constructors for both Pad and Ball . 如果由于某种原因你真的想要一个两阶段的初始化舞蹈,那么你需要为PadBall提供默认的构造函数。 I wouldn't recommend that though; 我不建议这样做; there's less scope for errors if an object can't be created in an invalid state. 如果无法在无效状态下创建对象,则可以减少错误的范围。

I think that the problem is that either class Pad or class Ball has no the default constructor ( you have two dtat members of these classes in the class definition of ArkanoidGame: Pad pad; and Ball ball;) . 我认为问题是Pad类或类Ball没有默认构造函数(在ArkanoidGame的类定义中有两个这些类的dtat成员:Pad pad;和Ball ball;)。 In this case the compiler defined the default constructor of class ArkanoidGame as deleted (otherwise it will be ill-formed). 在这种情况下,编译器将类ArkanoidGame的默认构造函数定义为已删除(否则它将是格式错误的)。 However in the first line of main 但是在第一线主力

ArkanoidGame game;

you try to call the default constructor of class ArkanoidGame. 您尝试调用类ArkanoidGame的默认构造函数。

Take also into account that declarations of member functions shall have unqualified names in the class definition. 还要考虑到成员函数的声明在类定义中应具有不合格的名称。 So for example this declaration 例如这个声明

void ArkanoidGame::init(int, int);

is invalid. 是无效的。 Shall be 应该

void init(int, int);

You should provide a constructor for ArkanoidGame. 你应该为ArkanoidGame提供一个构造函数。 In Arkanoid.h: 在Arkanoid.h:

ArkanoidGame ();

In Arkanoid.cpp: 在Arkanoid.cpp中:

ArkanoidGame::ArkanoidGame ()
{
    // it is better to initialize members in the constructor,
    // although not strictlynecessary
    running = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C2280尝试引用已删除的功能 - C2280 attempting to reference a deleted function C2280 = 试图引用已删除的 function - C2280 = attempting to reference a deleted function Qt编译错误:C2280:尝试引用已删除的函数 - Qt compile error: C2280: attempting to reference a deleted function C ++错误C2280:尝试引用已删除的函数 - C++ Error C2280: Attempting to reference a deleted function 错误C2280:尝试引用已删除的函数(原子 <int> ) - error C2280: attempting to reference a deleted function (atomic<int>) 编译器错误C2280,尝试引用已删除的函数operator = - Compiler error C2280, attempting to reference a deleted function operator= 错误C2280:尝试引用已删除的函数(unique_ptr) - Error C2280: attempting to reference a deleted function (unique_ptr) 错误C2280:&#39;std :: thread :: thread(const std :: thread&)&#39;:尝试引用已删除的函数 - Error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function Visual Studio 2013 和 2015 中的 C++ 编译器错误 C2280“试图引用已删除的函数” - C++ Compiler Error C2280 "attempting to reference a deleted function" in Visual Studio 2013 and 2015 错误C2280:在声明C ++结构时尝试引用已删除的函数 - error C2280: attempting to reference a deleted function while declaring a C++ struct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM