2015-12-14 66 views
0

對於我的比賽,我想用PhysFs提取是在一個zip文件R6025純虛函數調用(從舊金山派生類::的InputStream)

我創建了一個自定義類MusicStreamsf::InputStream繼承的音樂文件我用作sf::Music的流。

這是我的基本程序:

#include <SFML/Graphics.hpp> 
#include <SFML/Audio.hpp> 
#include "musicstream.h" 
#include "physfs.h" 

int main() { 
    PHYSFS_init(0); 
    PHYSFS_addToSearchPath("data.zip", 0); 

    std::string musicFile = "music.ogg"; 
    if (PHYSFS_exists(musicFile.c_str()) == 0) { 
    PHYSFS_deinit(); 
    return EXIT_FAILURE; 
    } 

    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!"); 
    sf::Music myMusic; 
    MusicStream myStream(musicFile.c_str()); 
    if (!myStream.getError()) { 
    myMusic.openFromStream(myStream); 
    myMusic.play(); 
    } 
    while (window.isOpen()) { 
    sf::Event event; 
    while (window.pollEvent(event)) { 
     if (event.type == sf::Event::Closed) window.close(); 
    } 
    } 

    myMusic.stop(); 

    PHYSFS_deinit(); 
    return 0; 
} 

這完美的作品,除了一兩件事:

當我關閉窗口,退出程序,我得到一個運行時錯誤R6025 pure virtual function call和程序崩潰。

所以顯然純粹的虛函數被稱爲(sf::InputStream's dtor ??),但我實現了sf::InputStream的所有功能,這對我來說沒有任何意義。

而且,我真的不知道,如果代碼是相關的,但如果它是,這是自定義類:

musicstream.h

#ifndef MUSIC_STREAM_H_INCLUDED 
#define MUSIC_STREAM_H_INCLUDED 

#include <SFML/System.hpp> 
#include "physfs.h" 

class MusicStream : public sf::InputStream { 
public: 
    MusicStream(); 
    MusicStream(const char *fileName); 
    virtual ~MusicStream() override; 

    sf::Int64 read(void *data, sf::Int64) override; 
    sf::Int64 seek(sf::Int64 position) override; 
    sf::Int64 tell() override; 
    sf::Int64 getSize() override; 

    bool getError() const; 

private: 
    PHYSFS_File *file_; 
    bool error_; 

}; 

#endif 

musicstream.cpp

#include "musicstream.h" 

MusicStream::MusicStream() : 
    error_(true) 
{ 
} 

MusicStream::MusicStream(const char *filename) : 
    error_(false) 
{ 
    file_ = PHYSFS_openRead(filename); 
    if (file_ == nullptr) { 
    error_ = true; 
    } 
} 

MusicStream::~MusicStream() { 
    if (error_) { return; } 
    PHYSFS_close(file_); 
} 

sf::Int64 MusicStream::read(void *data, sf::Int64 size) { 
    if (error_) { return 0; } 
    sf::Int64 fileRead = PHYSFS_read(file_, data, 1, size); 
    if (fileRead == -1) { 
    return 0; 
    } 
    return fileRead; 
} 

sf::Int64 MusicStream::seek(sf::Int64 position) { 
    if (error_) { return -1; } 
    if (PHYSFS_seek(file_, position) == 0) { 
    return -1; 
    } 
    return position; 
} 

sf::Int64 MusicStream::tell() { 
    if (error_) { return -1; } 
    sf::Int64 position = PHYSFS_tell(file_); 
    return position; 
} 

sf::Int64 MusicStream::getSize() { 
    if (error_) { return -1; } 
    sf::Int64 size = PHYSFS_fileLength(file_); 
    return size; 
} 

bool MusicStream::getError() const { 
    return error_; 
} 

回答

0

的問題是這兩條線:

sf::Music myMusic; 
MusicStream myStream(musicFile.c_str()); 

我交換了他們,擺脫了錯誤。這是因爲音樂是在自己的線程中播放的。它在摧毀後試圖從流中讀取。現在音樂在流之前被破壞。