【问题标题】:Attempting to reference a deleted function (VS2013)试图引用已删除的函数(VS2013)
【发布时间】:2015-08-30 03:17:06
【问题描述】:

我正在尝试在 SDL 中编写跳棋游戏。当我编译我的代码时,我得到了这个错误:

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

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

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();
        }
    }
}

【问题讨论】:

  • 除非你每隔一个下午实现 C++ 标准库,否则错误与 your 构造函数无关。你能确定那条消息中提到的类吗?
  • "此诊断发生在编译器生成的函数 Board::Board(const Board&)" 我认为这是我的板类。
  • 您显示的代码中哪里出现了错误?我无法复制粘贴此代码并使其工作。甚至不工作。您在这里缺少loaders.h。另外,请参阅sscce.org
  • 我在某处抄板,是的。我在它的位置做了其他事情,错误消失了。

标签: c++ sdl-2


【解决方案1】:

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

std::ifstream boardFile;

编译器提供的Board 复制构造函数尝试复制流,但流不可复制。因此,您必须提供自己的复制构造函数来做一些聪明的事情,或者删除需要Board 复制构造函数的代码。

【讨论】:

  • 将指向 ifstream 的指针用作类成员并在类的构造函数/析构函数中分配/解除分配 ifstream 是可接受的解决方案吗?
猜你喜欢
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多