将shared_ptr传递给std :: fstream * EDIT

时间:2011-03-26 11:00:23

标签: c++ boost shared-ptr boost-smart-ptr

我有一些问题要理解shared_ptr doc,因为我是c ++中的新手。我希望你能用我的示例代码帮助我:

#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.

我稍微调整了一下,发现问题是由

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

如果它不在函数open_file内,我也会得到相同的错误

2 个答案:

答案 0 :(得分: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 );
    }

答案 1 :(得分:1)

您可以使用shared_ptr成员获取get的基础指针。更好的选择是将ls_coeff_p设为shared_ptr

您也可以跳过临时变量并执行

*(ls_coeff_f[0][0]) << "Hello world" << std::endl;