| | |
constant element in std::vector
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 9
Reputation:
Solved Threads: 0
Hello,
Can anybody explain why I got an awful error message when try following code:
Thanks in advance,
brain.
Can anybody explain why I got an awful error message when try following code:
C++ Syntax (Toggle Plain Text)
#include <vector> int main() { std::vector<const int> v; return 0; }
brain.
Last edited by Narue; Jul 2nd, 2008 at 11:32 am. Reason: Added code tags
•
•
Join Date: Jan 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
What is the error-message? What compiler are you using?
This compiles fine on VS2008 with warninglevel 4.
And why do you want to use const ints?
I just want to keep the vector from constant integers.
The error message is the following:
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h: In instantiation of ‘__gnu_cxx::new_allocator<const int>’:
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/allocator.h:86: instantiated from ‘std::allocator<const int>’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:79: instantiated from ‘std::_Vector_base<const int, std::allocator<const int> >’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:160: instantiated from ‘std::vector<const int, std::allocator<const int> >’
test.cpp:18: instantiated from here
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h:81: error: ‘const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const [with _Tp = const int]’ cannot be overloaded
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h:78: error: with ‘_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const int]’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::deallocate(_Tp*, size_t) [with _Tp = const int]’:
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:134: instantiated from ‘void std::_Vector_base<_Tp, _Alloc>::_M_deallocate(_Tp*, size_t) [with _Tp = const int, _Alloc = std::allocator<const int>]’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:120: instantiated from ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = const int, _Alloc = std::allocator<const int>]’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:199: instantiated from ‘std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = const int, _Alloc = std::allocator<const int>]’
test.cpp:18: instantiated from here
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h:97: error: invalid conversion from ‘const void*’ to ‘void*’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h:97: error: initializing argument 1 of ‘void operator delete(void*)’
•
•
Join Date: Mar 2008
Posts: 41
Reputation:
Solved Threads: 7
Try this,
C++ Syntax (Toggle Plain Text)
#include <vector> #include<iostream> using namespace std; int main() { vector<const int*> v; return 0; }
Last edited by RenjithVR; Jul 3rd, 2008 at 6:59 am.
•
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
try
cpp Syntax (Toggle Plain Text)
#include <vector> int main() { std::vector<const int> v(); return 0; }
Last edited by ivailosp; Jul 3rd, 2008 at 7:51 am.
•
•
Join Date: Jan 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
try
cpp Syntax (Toggle Plain Text)
#include <vector> int main() { std::vector<const int> v(); return 0; }
Thanks. it helps.
But I cannot add any element to it.
v.push_back(5);
causes error message.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
concept: the value_type of a standard container (the type of the object stored in the container) is required to be Assignable http://www.sgi.com/tech/stl/Assignable.html
a
a
const T ( or for that matter a T& ) is not Assignable and cannot be the value_type in a standard container. Last edited by vijayan121; Jul 4th, 2008 at 8:31 am.
•
•
Join Date: Jan 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
concept: the value_type of a standard container (the type of the object stored in the container) is required to be Assignable http://www.sgi.com/tech/stl/Assignable.html
aconst T( or for that matter aT&) is not Assignable and cannot be the value_type in a standard container.
#include <vector>
int main()
{
std::vector<const int> v;
v.push_back(5);
v[0] = 2;
return 0;
}
I also would like to understand if following code cannot be compiled:
#include <vector>
#include <iostream>
void f(std::vector<int>::const_iterator& i)
{
std::cout << *i << std::endl;
}
int main()
{
std::vector<int> v;
for (std::vector<int>::iterator b = v.begin(); b != v.end(); ++b)
f(b);
return 0;
}
but if the prototype of the function is the following everything is OK.
void f(const std::vector<int>::const_iterator& i); or
void f(std::vector<int>::const_iterator i);
Regards,
brain.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
for a standard container, begin() and end() are overloaded on the const specifier.
on a modifiable container, begin() and end() return container::iterator.
on a const container, begin() and end() returns container::const_iterator.
this code will not compile:
but this will:
c++09: containers also have:
http://www.open-std.org/jtc1/sc22/wg...2004/n1674.pdf
so things become a bit more convenient.
note: prefer passing an iterator by value (rather than by modifiable reference);
it is more flexible for iterations to be non-intrusive.
on a modifiable container, begin() and end() return container::iterator.
on a const container, begin() and end() returns container::const_iterator.
C++ Syntax (Toggle Plain Text)
template< typename CNTR > // CNTR is a standard container void foo( CNTR& cntr, const CNTR& const_cntr ) { typename CNTR::iterator iter = cntr.begin() ; typename CNTR::const_iterator const_iter = const_cntr.begin() ; typename CNTR::reverse_iterator rev_iter = cntr.rbegin() ; typename CNTR::const_reverse_iterator const_rev_iter = const_cntr.rbegin() ; }
this code will not compile:
C++ Syntax (Toggle Plain Text)
#include <vector> void f( std::vector<int>::const_iterator& i ) { std::cout << *i << std::endl; } int main() { std::vector<int> v; std::vector<int>::iterator b = v.begin() ; f(b); }
C++ Syntax (Toggle Plain Text)
template< typename ITERATOR > void f( ITERATOR i ) { std::cout << *i << std::endl; } #include <vector> int main() { std::vector<int> v; std::vector<int>::iterator b = v.begin() ; f(b); }
C++ Syntax (Toggle Plain Text)
// ... const_iterator cbegin() const; const_iterator cend () const; const_reverse_iterator crbegin() const; const_reverse_iterator crend () const; // ...
so things become a bit more convenient.
note: prefer passing an iterator by value (rather than by modifiable reference);
it is more flexible for iterations to be non-intrusive.
•
•
Join Date: Jan 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
for a standard container, begin() and end() are overloaded on the const specifier.
on a modifiable container, begin() and end() return container::iterator.
on a const container, begin() and end() returns container::const_iterator.
C++ Syntax (Toggle Plain Text)
template< typename CNTR > // CNTR is a standard container void foo( CNTR& cntr, const CNTR& const_cntr ) { typename CNTR::iterator iter = cntr.begin() ; typename CNTR::const_iterator const_iter = const_cntr.begin() ; typename CNTR::reverse_iterator rev_iter = cntr.rbegin() ; typename CNTR::const_reverse_iterator const_rev_iter = const_cntr.rbegin() ; }
this code will not compile:
but this will:C++ Syntax (Toggle Plain Text)
#include <vector> void f( std::vector<int>::const_iterator& i ) { std::cout << *i << std::endl; } int main() { std::vector<int> v; std::vector<int>::iterator b = v.begin() ; f(b); }
c++09: containers also have:C++ Syntax (Toggle Plain Text)
template< typename ITERATOR > void f( ITERATOR i ) { std::cout << *i << std::endl; } #include <vector> int main() { std::vector<int> v; std::vector<int>::iterator b = v.begin() ; f(b); }
http://www.open-std.org/jtc1/sc22/wg...2004/n1674.pdfC++ Syntax (Toggle Plain Text)
// ... const_iterator cbegin() const; const_iterator cend () const; const_reverse_iterator crbegin() const; const_reverse_iterator crend () const; // ...
so things become a bit more convenient.
note: prefer passing an iterator by value (rather than by modifiable reference);
it is more flexible for iterations to be non-intrusive.
![]() |
Similar Threads
- fibonacci (C++)
- arrays, addition, and fibonacci (C++)
Other Threads in the C++ Forum
- Previous Thread: fopen
- Next Thread: How do I write code for PAC MAN!
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






