2011-03-26 119 views
0

我有一些問題需要理解shared_ptr doc,因爲我是C++中的新手。我希望你能幫助我,我的示例代碼:將shared_ptr傳遞給std :: fstream *編輯

#include <iomanip> 
#include <string> 
#include <iostream> 
#include <boost/shared_ptr.hpp> 
#include <boost/multi_array.hpp> 
#include <boost/lexical_cast.hpp> 
#include <cstdlib> 
#include <fstream> // file I/O 
#include <sstream> 

void open_file(boost::shared_ptr<std::fstream> &file_stream , const std::string file_name , uint8_t presition); 

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

    uint32_t   num_bits = 12; 
    std::fstream  *ls_coeff_p; 
    boost::multi_array< boost::shared_ptr< std::fstream> , 2> ls_coeff_f(boost::extents[ num_bits ][ 2 ]); 

    std::string ls_coeff_file_name = "datafiles/ls_coeff"; 
    std::string stable    = "_stable_"; 
    std::string unstable   = "_unstable_"; 
    std::string file_name_end  = ".log"; 

    for (uint8_t i = 0; i < num_bits ; i++) {    
      open_file(ls_coeff_f[i][0] , ls_coeff_file_name + unstable + boost::lexical_cast<std::string>(i) + file_name_end , 4); 
      open_file(ls_coeff_f[i][1] , ls_coeff_file_name + stable + boost::lexical_cast<std::string>(i) + file_name_end , 4); 
    } 

    // just as test case 
    ls_coeff_p = ls_coeff_f[0][0]; 

    *ls_coeff_p << "Hallo world!" << std::endl; 

    for (uint8_t i = 0; i < num_bits ; i++) { 
      ls_coeff_f[i][0]->close(); 
      ls_coeff_f[i][1]->close(); 
    } 

} 

void open_file(boost::shared_ptr<std::fstream> &file_stream , const std::string file_name , uint8_t presition) { 

    file_stream->open (file_name , std::fstream::out); 
    file_stream->precision(presition); 
    file_stream->setf(std::ios::fixed,std::ios::floatfield); 

} 

我得到以下錯誤:

In function 'int main(int, char**)': 
Line 30: error: cannot convert 'boost::shared_ptr<std::basic_fstream<char, std::char_traits<char> > >' to 'std::fstream*' in assignment 
compilation terminated due to -Wfatal-errors. 

問候

編輯

底肥@ Space_C0wb0y的建議之後, :

#include <iomanip> 
#include <string> 
#include <iostream> 
#include <boost/shared_ptr.hpp> 
#include <boost/multi_array.hpp> 
#include <boost/lexical_cast.hpp> 
#include <cstdlib> 
#include <fstream> // file I/O 
#include <sstream> 

void open_file(boost::shared_ptr<std::fstream> &file_stream , const std::string file_name , uint8_t presition); 

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

    uint32_t   num_bits = 12; 
    std::fstream  *ls_coeff_p; 
    boost::multi_array< boost::shared_ptr< std::fstream> , 2> ls_coeff_f(boost::extents[ num_bits ][ 2 ]); 

    std::string ls_coeff_file_name = "datafiles/ls_coeff"; 
    std::string stable    = "_stable_"; 
    std::string unstable   = "_unstable_"; 
    std::string file_name_end  = ".log"; 

    for (uint8_t i = 0; i < num_bits ; i++) {    
      open_file(ls_coeff_f[i][0] , ls_coeff_file_name + unstable + boost::lexical_cast<std::string>(i) + file_name_end , 4); 
      open_file(ls_coeff_f[i][1] , ls_coeff_file_name + stable + boost::lexical_cast<std::string>(i) + file_name_end , 4); 
} 

    // just as test case 
    ls_coeff_p = ls_coeff_f[0][0].get(); 

    *ls_coeff_p << "Hallo world!" << std::endl; 

    for (uint8_t i = 0; i < num_bits ; i++) { 
     ls_coeff_f[i][0]->close(); 
     ls_coeff_f[i][1]->close(); 
    } 

} 

void open_file(boost::shared_ptr<std::fstream> &file_stream , const std::string file_name , uint8_t presition) { 

    file_stream->open (file_name.c_str() , std::fstream::out); 
    file_stream->precision(presition); 
    file_stream->setf(std::ios::fixed,std::ios::floatfield); 

} 

我得到以下錯誤:

t: /usr/local/include/boost/shared_ptr.hpp:315: T* boost::shared_ptr<T>::operator->() const [with T = std::basic_fstream<char, std::char_traits<char> >]: Assertion `px != 0' failed. 

我debuged它了一下,發現了該問題在

file_stream->open (file_name.c_str() , std::fstream::out); 

也引起了,如果它不是在函數內部open_file我得到相同錯誤

+1

的答案是完全正常的,僅有2註釋:您將獲得運行時錯誤不inizializing您的指針。作爲一般指導原則,請儘量保持您使用的指針的一致性。如果共享指針超出範圍或者「刪除」指針,則使用指向共享資源的指針可能會造成麻煩。 – AndreasT 2011-03-26 11:22:11

+0

@AndreasT,這是我忘了的inizializion行:'ls_coeff_f [i] [0] = boost :: shared_ptr (new std :: fstream);'這是我怎麼想寫的?要不然? – Eagle 2011-03-26 16:34:31

+0

我編輯了我的答案來涵蓋。 – 2011-03-26 20:50:03

回答

1

訪問原始指針在shared_ptr,你必須使用get - 方法:

ls_coeff_p = ls_coeff_f[0][0].get(); 

關於您的編輯:

您必須初始化共享指針數組中,然後才能使用它們。這裏是我會做:

boost::shared_ptr<std::fstream> open_file(const std::string file_name , 
              uint8_t presition) { 
    boost::shared_ptr<std::fstream> stream = boost::make_shared<std::fstream>( 
     file_name.c_str(), std::fstream::out) 
    stream->precision(presition); 
    stream->setf(std::ios::fixed,std::ios::floatfield); 
    return stream; 
} 

for (uint8_t i = 0; i < num_bits ; i++) { 
      ls_coeff_f[i][0] = open_file(ls_coeff_file_name + unstable + 
              boost::lexical_cast<std::string>(i) + 
              file_name_end , 4); 
      ls_coeff_f[i][1] = open_file(ls_coeff_file_name + stable + 
              boost::lexical_cast<std::string>(i) + 
              file_name_end , 4); 
    } 
+0

更改了我的代碼,並得到:第43行:錯誤:'class boost :: shared_ptr >'沒有名爲'open'的成員 – Eagle 2011-03-26 11:13:19

+0

您必須使用'file_name.c_str ()'傳遞文件名。 – 2011-03-26 11:22:33

1

你可以得到一個shared_ptr的根本指針與get成員。更好的選擇是製作ls_coeff_p a shared_ptr

您也可以跳過臨時變量並做

*(ls_coeff_f[0][0]) << "Hello world" << std::endl; 
相關問題