尝试引用已删除的函数

尝试引用已删除的函数

本文介绍了尝试引用已删除的函数(VS2013)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 SDL 编写跳棋游戏.当我编译我的代码时,我收到此错误:

I'm trying to write a game of checkers in SDL. When I compile my code, I get this error:

std::basic_ifstream>::basic_ifstream(conststd::basic_ifstream> &)' : 试图引用已删除的函数

从我在网上找到的信息来看,这意味着编译器出于某种原因删除了我的构造函数,现在它再也找不到了.(如果你问我,组织不好)为什么会这样?

From what I can find on the web, this means that the compile has helpfully deleted my constructor for some reason, and now it can't find it again. (Bad organization if you ask me) Why could this be?

Board.h:

#include <fstream>
class Board
{
public:
    SDL_Surface * boardSurface;
    int boardArray[8][8];
private:
    std::ifstream boardFile;
    SDL_Surface * blackPiece;
    SDL_Surface * whitePiece;
    SDL_Surface * darkSquare;
    SDL_Surface * lightSquare;

public:
    Board(char filename[], SDL_PixelFormat * format);
private:
    void loadFile(char filename[]);
    void makeSurface();
    void debugPrint();
    void debugBlit();
};

Board.cpp:

#include <SDL.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include "board.h"
#include "loaders.h"

Board::Board(char filename[], SDL_PixelFormat * format)
{
    //inits images
    loaders imageLoader;
    blackPiece = imageLoader.load_image("images/blackPiece.png", format);
    whitePiece = imageLoader.load_image("images/whitePiece.png", format);
    darkSquare = imageLoader.load_image("images/darkSquare.png", format);
    lightSquare = imageLoader.load_image("images/lightSquare.png", format);
    boardSurface = SDL_CreateRGBSurface(0, 780, 480, 8, 0, 0, 0, 0);
    loadFile(filename);
    debugPrint();
    debugBlit();
}
void Board::loadFile(char filename[])
{
    boardFile.open(filename);
    char currentLine[9] = {};
    for (int line = 0; line <= 7; line++)
    {
        boardFile.getline(currentLine, 9);
        for (int square = 0; square <= 7; square++)
        {
            int currentSquare = (int)currentLine[square] - '0';
            boardArray[line][square] = currentSquare;
        }
    }
}

void Board::makeSurface()
{

}

void Board::debugPrint()
{
    for (int line = 0; line <= 7; line++)
    {
        for (int square = 0; square <= 7; square++)
        {
            std::cout << boardArray[line][square];
        }
        std::cout << std::endl;
    }
}

void Board::debugBlit()
{
    for (int y = 0; y <= 4; y++)
    {
        if (SDL_BlitSurface(blackPiece, NULL, boardSurface, NULL) != 0)
        {
            std::cout << SDL_GetError();
        }
    }
}

推荐答案

发生错误是因为您有一个 std::ifstream 数据成员,而您可能正试图复制一个 Board 某处,或者有一些需要复制构造函数才能访问的代码.

The error happens because you have an std::ifstream data member, and you are probably trying to copy a Board somewhere, or have some code that requires the copy constructor to be accessible.

std::ifstream boardFile;

Board 编译器提供的复制构造函数尝试复制流,但该流不可复制.所以你要么提供你自己的复制构造函数来做一些聪明的事情,要么删除需要 Board 复制构造函数的代码.

The Board compiler-provided copy constructor tries to copy the stream, but the stream is not copyable. So you have to either provide your own copy constructor to do something clever, or remove code that requires the Board copy constructor.

这篇关于尝试引用已删除的函数(VS2013)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:14