I have the following c++ code and I try to compile it with "g++ -c myCode.cpp" in three different versions of the gcc compiler:
------------------------------------------------------------
#include<vector>
using namespace std;
int main(){
vector<vector<int> > myVec;
//vector<int> myVec;
myVec.assign(5,0);
return 0;
}
------------------------------------------------------------
System 1: Fedora Core release 3, gcc version 4.0.1
-- Successful compilation
System 2: CentOS release 5.2, gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)
-- Successful compilation
System 3: Fedora release 10, gcc version 4.3.2 20081105 (Red Hat 4.3.2-7) (GCC)
-- Compilation error as below:
/usr/lib/gcc/x86_64-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/stl_vector.h:992: error: no matching function for call to \u2018std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_fill_assign(int&, int&)\u2019
/usr/lib/gcc/x86_64-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/vector.tcc:183: note: candidates are: void std::vector<_Tp, _Alloc>::_M_fill_assign(size_t, const _Tp&) [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]
But while I change vector<vector<int> > to vector<int>, all three compilers are comfortable.
I want to know the reason for that. Please advise how to get rid of the compilation error. Thanks in advance.
Atanu is online now Report Post Edit/Delete Message
I want to allocate a vector<vector<int> > dynamically. So I declared a vector<vector<int> > myVec. In the runtime the size of myVec (say, r and c) is determined. At that point of time I allocate space for the vector.
You are trying to assign contents of myVec to 0...but the elements of myVec are vectors. You cannot assign a vector to 0.
If you want to allocate space for a certain amount of vectors, either use .resize() or .reserve().
Also, I doubt this was your intent, but using bolds like that kind of makes you seem pushy, or rude. Try not to use bolds like that in the future. I am sure we could have understood your intent without them.