This is a simple and complex question at the same time.
This compiles:
int Test;
vector<int> TEST;
TEST.push_back(Test);
cout << TEST.size();
This does not compile:
fstream Test;
vector<fstream> TEST;
TEST.push_back(Test);
cout << TEST.size();
Is there any particular reason?
Is there a way for me to get a dynamic list of fstreams?
The error message:
1>------ Build started: Project: vector_test, Configuration: Debug Win32 ------
1> vector_test.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(1347): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_fstream<_Elem,_Traits>::basic_fstream(const std::basic_fstream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The object fstream is not copyable.
If you need to record fstreams in a vector you can declare a std::vector<std::fstream*> and push back the address of the object. Remember that if you save the pointer, then you must ensure that, when you access it, the object is still alive.
In C++ 2011 the concrete stream objects are movable. Howver, to take advantage of this you either need to pass a temporary or allow the object to be moved:
std::vector<std::ofstream> files;
files.push_back(std::ofstream("f1"));
std::ofstream file("f2");
files.push_back(std::move(file));
Note that you can't use the file variable after this as the stream was moved into files.
To use a class with a vector, it must be copyable. fstream is not.
See: C++ std::ifstream in constructor problem
Edit: If you need to have multiple references to the same fstream, you can use shared_ptr to manage them. Try something like:
std::vector< std::shared_ptr<fstream> > TEST
A basic requirement for a type to be pushed into vector is that the object should be copyable, fstream is not copyable and hence you get compiler errors.
Like others have mentioned, fstream is not copyable. You could just do:
vector<fstream> TEST;
TEST.push_back(fstream()); //fstream is created in the vector, no copy needed
And use like this:
fstream& A = TEST[0];
And even iterate like this:
for(fstream& S : TEST){
...
}
Related
This question already has answers here:
Why is this code trying to call the copy constructor?
(2 answers)
Closed 4 years ago.
It's taken me a while, but I've finally constructed a minimal example with illustrates the problem I'm having.
#include <memory>
#include <vector>
class Thing
{
};
class Box
{
public:
std::vector<std::unique_ptr<Thing>> Things;
static Box MakeBox() {Box b; return b;}
};
My real program is obviously quite a lot more complicated than this.
GCC 4.8.3 happily compiles this. It also compiles the real application, which works perfectly.
Visual Studio 2012 insists that this code is not correct, giving me error C2248 on line 606 of vc\include\xmemory0. If I wade through several miles of compiler output, I discover the real source of the error is line 11 in the above example. (The line that defines Things.) VS also refuses to compile my real application, with the same error.
So, is this code correct or not? If it's not correct, then why does GCC accept it? And how to I make it correct? If it is correct, then why won't VS compile it? Is there some way I can unconditionally force VS to actually compile my program?
Output from VS:
1>------ Build started: Project: TestUniquePtr, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(606): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1> with
1> [
1> _Ty=Thing
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1> with
1> [
1> _Ty=Thing
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::allocator<std::unique_ptr<Thing>>
1> ]
1> d:\projects\c++\testuniqueptr\testuniqueptr\main.cpp(11) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Your problem isn't std::unique_ptr but std::vector.
Your compiler comes with an old version of std::vector that requires the element type to be copyable.
The return by value (return b;) should invoke a move of the vector, but your std::vector doesn't implement move.
std::unique_ptr is moveable but not copyable, therefore it doesn't meet the pre-C++11 requirements for being used in std::vector... a requirement which still applies to VC++ 2012.
Your best option is to use a newer compiler and standard library. One that supports move semantics on std::vector.
Otherwise you might make some progress by eliminating the copy of std::vector, for example, by having MakeBox fill in an output argument rather than returning a new object.
static void MakeBox(Box& b) { /* fill Things in b provided by caller */ }
That's probably an exercise in futility though, because whenever the vector needs to grow, it has to relocate the existing elements to new storage, and with incomplete move support, it will try to copy those.
The problem is that Box has no move constructor, thus returning a Box requires it to have a copy constructor (which it can't because unique_ptr is not copyable). All you have to do is define a move constructor for Box:
Box::Box(Box&& other)
: Things(std::move(other.Things))
{
}
With more recent editions, the compiler will generate the move constructor for you.
First of all, I use boost library, and if it changes anything, the code is compiled on a Windows Machine.
The code itself contains a lot more of function acting upon matrices but only this one triggers the error.
Well, I am trying to transform matrix like :
{001
100
010}
To something like :
{1
3
2}
But strangely I can't compile my code and I can't find the error so I would be glad if anyone could help me.
Below the code :
using namespace boost::numeric::ublas;
typedef matrix <float, row_major, unbounded_array<float>> MATRIXf;
MATRIXf matrix_to_class (const MATRIXf inputM)
{
MATRIXf output;
for (std::size_t line = 0; line < inputM.size1(); line++)
{
for (std::size_t column = 0; column < inputM.size2(); column++)
{
if (column == 1)
{
output.insert_element(line,0.0,column);
}
}
}
return output;
}
Here is the error code:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): error C4996: 'std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): note: see declaration of 'std::copy::_Unchecked_iterators::_Deprecate'
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(204): note: see reference to function template instantiation '_OutIt *std::copy<float*,float*>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=float *,
1> _InIt=float *
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(201): note: while compiling class template member function 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>> &boost::numeric::ublas::unbounded_array<T,std::allocator<T>>::operator =(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)'
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(310): note: see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>> &boost::numeric::ublas::unbounded_array<T,std::allocator<T>>::operator =(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)' being compiled
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(102): note: see reference to class template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>' being compiled
1> with
1> [
1> T=float
1> ]
1> g:\c++ python\travail\visualstudio\visualstudio\guigui\neural net\neural net\utils.hpp(21): note: see reference to class template instantiation 'boost::numeric::ublas::matrix<float,boost::numeric::ublas::row_major,boost::numeric::ublas::unbounded_array<float,std::allocator<T>>>' being compiled
1> with
1> [
1> T=float
1> ]
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory(102): error C4996: 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate': Call to 'std::uninitialized_copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory(102): note: see declaration of 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate'
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(94): note: see reference to function template instantiation '_FwdIt *std::uninitialized_copy<const float*,float*>(_InIt,_InIt,_FwdIt)' being compiled
1> with
1> [
1> _FwdIt=float *,
1> _InIt=const float *
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(89): note: while compiling class template member function 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>::unbounded_array(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)'
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(162): note: see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>::unbounded_array(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)' being compiled
1> with
1> [
1> T=float
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Trying to locate the error brings me to the function above.
Thanks in advance.
Near the end of the first error message is the text, "To disable this warning, use -D_SCL_SECURE_NO_WARNINGS". https://msdn.microsoft.com/en-us/library/ttcz0bys.aspx has a more detailed discussion of warnings and checked iterators. In essence, Microsoft created "safe" mutations of Standard C++ functions to help developers avoid invalid iterator usage. The error message suggests that you define _SCL_SECURE_NO_WARNINGS. This can be done in the project properties C/C++/Preprocessor/Preprocessor Definitions. In a project I worked on in the past, we disabled all the "safe" versions of the functions because of the performance hit.
You may be interested in reading the above Microsoft page for more information about the checked iterator topic.
I'm using visual studio 2012 and I isolated a problem in my code to this, but I can't solve it. When running it in release mode it works perfect, but I get an error if I run it in debug.
The code is:
#include "stdafx.h"
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
typedef boost::numeric::ublas::matrix<double> BMT;
BMT fun()
{
BMT mym;
mym.resize(3,3);
for(int i = 0; i<9;++i) mym(i/3,i%3)=i;
std::cout << mym << std::endl;
return mym;
}
int main(int argc, char* argv[])
{
fun();
//closing message
std::cout<<std::endl<<"press enter to exit."<<std::endl;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
and the error in debug is the following:
1>------ Build started: Project: myproject, Configuration: Debug x64 ------
1> myapp.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory(348): error C4996: 'std::_Uninitialized_copy0': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory(333) : see declaration of 'std::_Uninitialized_copy0'
1> C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/storage.hpp(94) : see reference to function template instantiation '_FwdIt std::uninitialized_copy<const double*,double*>(_InIt,_InIt,_FwdIt)' being compiled
1> with
1> [
1> _FwdIt=double *,
1> _InIt=const double *
1> ]
1> C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/storage.hpp(89) : while compiling class template member function 'boost::numeric::ublas::unbounded_array<T>::unbounded_array(const boost::numeric::ublas::unbounded_array<T> &)'
1> with
1> [
1> T=double
1> ]
1> C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/matrix.hpp(160) : see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<T>::unbounded_array(const boost::numeric::ublas::unbounded_array<T> &)' being compiled
1> with
1> [
1> T=double
1> ]
1> C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/matrix.hpp(100) : see reference to class template instantiation 'boost::numeric::ublas::unbounded_array<T>' being compiled
1> with
1> [
1> T=double
1> ]
1> junkApp1.cpp(10) : see reference to class template instantiation 'boost::numeric::ublas::matrix<T>' being compiled
1> with
1> [
1> T=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Do you know what can the problem be?
I was having a similar issue using ublas.
I am not sure what the cause might be but I suppose it's got to do with ublas' copy on write / copy on demand optimizations. Would it be an option to just use the define -D_SCL_SECURE_NO_WARNINGS and be done with it? I have that set globally as I consider 90% of those warnings OS specifc BS anyway.
The problem is that you are compiling with warnings treated as errors.
EDIT: Microsoft has decided that certain parts of C++ (and C) are deprecated, and the compiler reports use of those parts as errors unless _SCL_SECURE_NO_WARNINGS (respectively _CRT_SECURE_NO_WARNINGS) is defined.
I get these errors when trying to do the following.
I have a FileMgr class to handle an input and an output file with two member functions to copy each line of input into a list and to write to the output from each member of a list. note: the following functions work properly when handled directly by my main! so don't bother trying to make out what I'm doing with the copy functions, I spent a lot of time figuring them out and now they work fine, the problem is not there.
FileMgr::FileMgr(string inFilename, string outFilename)
{
input.open(inFilename);
output.open(outFilename);
}
bool FileMgr::writeFileToList(list<string> &l)
{
// copy each line of file into new member of list<string>
if(!input.is_open())
return false;
copy(istream_iterator<string>(input), istream_iterator<string>(), back_inserter(l));
return true;
}
bool FileMgr::writeListToFile(list<string>::iterator begin, list<string>::iterator end)
{
// copy each member of list<string> in output file, beginning and ending at iterators begin, end
// note that I have to pass a "false" end iterator, that is, end--, for it to work
if(!output.is_open())
return false;
copy(begin, end, ostream_iterator<string>(output, "\n"));
return true;
}
and up to here everything is fine. then my other class, which gets the list from FileMgr, and it's supposed to let the user edit it (im not there yet because of these errors), so heres part of my declaration:
class Dictionary
{
public:
Dictionary(string inFileName = "dictionary.txt", string outFileName = "output.txt");
void userEditor();
//private:
list<string> dictionary;
FileMgr manager;
bool findWord(string word);
bool addWord(string word);
bool deleteWord(string word);
void sortAndFix();
void saveAndExit();
and here's my definitions so far, which is basically just the constructor:
Dictionary::Dictionary(string inFileName, string outFileName)
{
// open files and copy to list; sort and fix list.
manager = FileMgr(inFileName, outFileName);
dictionary.push_back(" ");
if( manager.writeFileToList(dictionary) )
cout << "File successfully read from " << inFileName << endl;
else
cout << "Error in reading " << inFileName << endl;
sortAndFix();
}
when I compile, I get these errors somewhere unknown in the constructor just shown (because it's the only code in the file I get these errors from when compiling):
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(860): error
C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator =(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(604): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
I can't understand what's wrong. my FileMgr works fine when tested from my main, so why would the compiler trip like that when working with FileMgr from another class??
I believe that your problem is in this line:
manager = FileMgr(inFileName, outFileName);
From your code in FileMgr it seems that FileMgr has a stream input as a data member. When you execute the above line, you'll invoke the assignment operator for FileMgr, which by default will try to copy all of the data members one at a time. However, the copy functions for streams are not accessible (they're marked private and not implemented). The errors you're getting are almost certainly due to the C++ compiler noticing that it needs to copy the streams, but failing to do so because the copy functions are inaccessible.
To change this, try initializing manager in the constructor's member initializer list:
Dictionary::Dictionary(string inFileName, string outFileName)
: manager(inFileName, outFileName) {
/* ... */
}
This will initialize manager with the given parameters rather than trying to assign manager an object with the right parameters.
Hope this helps!
bool FileMgr::writeFileToList(list<string> &l);
FileMgr::writeFileToList receive an argument of type list<string> by reference.
So you should actually do -
list<string> dictionary;
if( manager.writeFileToList(&dictionary) )
^ error. You should not use & symbol here.
The argument type is not list<string>*l to send an address.
Just write:
if( manager.writeFileToList(dictionary) );
& is not needed. In fact, that causes the error!
BTW, your std::copy is incorrectly written. Here is the correct one:
copy(istream_iterator<string>(input), istream_iterator<string>(), std::back_inserter(l));
Note the last argument. It's std::back_inserter(l).
if( manager.writeFileToList(&dictionary) ) should be changed to
if( manager.writeFileToList(dictionary) )
Note you cant convert Type* to Type&
I want to use a vector to hold read-only integer-matrices of the size 5x5
vector<const int[5][5]> startingPieces;
But this declaration causes a bunch of weird errors I've never ever seen before.
error C2535: 'const int (*std::allocator<_Ty>::address(const int (&)[5][5]) const)[5][5]' : member function already defined or declared
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\xmemory(109) : see declaration of 'std::allocator<_Ty>::address'
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\vector(429) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\vector(439) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
1> with
1> [
1> _Ty=const int [5][5],
1> _Alloc=std::allocator<const int [5][5]>
1> ]
1> c:\users\eric\documents\visual studio 2008\projects\testing grounds\testing grounds\main.cpp(14) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=const int [5][5]
1> ]
So, what is wrong with this declaration?
Two things - firstly vectors cannot hold const objects - see Can I use const in vectors to allow adding elements, but not modifications to the already added? for a discussion of this. And secondly they cannot hold arrays, as the things they hold must be copyable and assignable, and arrays are neither.
What you should do here is create your own matrix class that stores the 5x5 array of data and then create your vector with that.
One option is to use the array class (your implementation may support std::array or std::tr1::array; if not, you can use boost::array from the Boost libraries):
std::vector<std::array<std::array<int, 5> > >
The elements stored in the container still cannot be const; you can make the entire vector const if that works for your use case.