c++ - 将 shared_ptr 传递给 std::fstream * 编辑

标签 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 内,我也会得到同样的错误

最佳答案

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

你也可以跳过临时变量做

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

关于c++ - 将 shared_ptr 传递给 std::fstream * 编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5441858/

相关文章:

c++ - 模板约束是否可用于变量模板?

c++ - MS群集-如何初始化RESUTIL_PROPERTY_ITEM结构?

c++ - 正确使用右值引用作为参数

boost - 执行 bcp 后构建依赖的 boost 库

c++ - boost::intrusive 中的线程安全保证

c++ - 如何将 n 个参数传递给 n 未知的函数

linux - 更新库 : Boost on Debian 7

c++ - 在 dll 接口(interface)中使用 shared_ptr

c++ - 从 std::shared_ptr<void> 初始化 std::shared_ptr<T>

c++ - C++中如何同时使用自动引用计数和虚函数?