将shared_ptr传递给std::fstream * 编辑

发布于 2024-10-26 23:36:33 字数 4519 浏览 3 评论 0原文

我在理解 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 内部也会

I had some problem to understand the shared_ptr doc since I am newbie in c++. I hope you could help me with my example code:

#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);

}

i get the following error:

 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.

Regards

EDIT

after appling the suggestions of @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);

}

i get the following error:

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.

i debuged it a bit and found out the the problem is caused in

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

also if it is not inside the function open_file I get the same error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-11-02 23:36:33

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

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

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

You can get the underlying pointer of a shared_ptr with the get member. A better option would be to make ls_coeff_p a shared_ptr as well.

You can also skip the temporary variable and do

*(ls_coeff_f[0][0]) << "Hello world" << std::endl;
毅然前行 2024-11-02 23:36:33

要访问 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 );
    }

To access the raw pointer in a shared_ptr you have to use the get-method:

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

About your edit:

You have to initialize the shared pointers in the array before you can use them. Here is how I'd do it:

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 );
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文