У меня были некоторые проблемы с пониманием 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.
Привет
EDIT
после применения предложений @ 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
Я получаю ту же ошибку