943,844 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4576
  • C++ RSS
Jul 2nd, 2008
0

constant element in std::vector

Expand Post »
Hello,

Can anybody explain why I got an awful error message when try following code:
C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2.  
  3. int main()
  4. {
  5. std::vector<const int> v;
  6. return 0;
  7. }
Thanks in advance,
brain.
Last edited by Narue; Jul 2nd, 2008 at 11:32 am. Reason: Added code tags
Similar Threads
Reputation Points: 6
Solved Threads: 0
Newbie Poster
brain is offline Offline
9 posts
since Jan 2008
Jul 2nd, 2008
0

Re: constant element in std::vector

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?
Last edited by Nick Evan; Jul 2nd, 2008 at 11:16 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jul 2nd, 2008
0

Re: constant element in std::vector

Click to Expand / Collapse  Quote originally posted by niek_e ...
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 use gcc version 4.2.2 20070909 (prerelease) (4.2.2-0.RC.1mdv2008.0).

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*)’
Reputation Points: 6
Solved Threads: 0
Newbie Poster
brain is offline Offline
9 posts
since Jan 2008
Jul 3rd, 2008
0

Re: constant element in std::vector

Try this,
C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. vector<const int*> v;
  7. return 0;
  8. }
Last edited by RenjithVR; Jul 3rd, 2008 at 6:59 am.
Reputation Points: 12
Solved Threads: 7
Light Poster
RenjithVR is offline Offline
41 posts
since Mar 2008
Jul 3rd, 2008
0

Re: constant element in std::vector

try
cpp Syntax (Toggle Plain Text)
  1. #include <vector>
  2.  
  3. int main() {
  4. std::vector<const int> v();
  5. return 0;
  6. }
Last edited by ivailosp; Jul 3rd, 2008 at 7:51 am.
Reputation Points: 21
Solved Threads: 22
Junior Poster
ivailosp is offline Offline
129 posts
since Apr 2008
Jul 4th, 2008
0

Re: constant element in std::vector

Click to Expand / Collapse  Quote originally posted by ivailosp ...
try
cpp Syntax (Toggle Plain Text)
  1. #include <vector>
  2.  
  3. int main() {
  4. std::vector<const int> v();
  5. return 0;
  6. }

Thanks. it helps.

But I cannot add any element to it.
v.push_back(5);

causes error message.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
brain is offline Offline
9 posts
since Jan 2008
Jul 4th, 2008
0

Re: constant element in std::vector

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 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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jul 4th, 2008
0

Re: constant element in std::vector

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
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 const T ( or for that matter a T& ) is not Assignable and cannot be the value_type in a standard container.
Ok, thank you, but I was confused because Visual C++ 2005 compiler lets the following code:

#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.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
brain is offline Offline
9 posts
since Jan 2008
Jul 5th, 2008
1

Re: constant element in std::vector

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)
  1. template< typename CNTR > // CNTR is a standard container
  2. void foo( CNTR& cntr, const CNTR& const_cntr )
  3. {
  4. typename CNTR::iterator iter = cntr.begin() ;
  5. typename CNTR::const_iterator const_iter = const_cntr.begin() ;
  6. typename CNTR::reverse_iterator rev_iter = cntr.rbegin() ;
  7. typename CNTR::const_reverse_iterator const_rev_iter = const_cntr.rbegin() ;
  8. }

this code will not compile:
C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2.  
  3. void f( std::vector<int>::const_iterator& i )
  4. { std::cout << *i << std::endl; }
  5.  
  6. int main()
  7. {
  8. std::vector<int> v;
  9. std::vector<int>::iterator b = v.begin() ;
  10. f(b);
  11. }
but this will:
C++ Syntax (Toggle Plain Text)
  1. template< typename ITERATOR > void f( ITERATOR i )
  2. { std::cout << *i << std::endl; }
  3.  
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8. std::vector<int> v;
  9. std::vector<int>::iterator b = v.begin() ;
  10. f(b);
  11. }
c++09: containers also have:
C++ Syntax (Toggle Plain Text)
  1. // ...
  2. const_iterator cbegin() const;
  3. const_iterator cend () const;
  4. const_reverse_iterator crbegin() const;
  5. const_reverse_iterator crend () const;
  6. // ...
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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jul 10th, 2008
0

Re: constant element in std::vector

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
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)
  1. template< typename CNTR > // CNTR is a standard container
  2. void foo( CNTR& cntr, const CNTR& const_cntr )
  3. {
  4. typename CNTR::iterator iter = cntr.begin() ;
  5. typename CNTR::const_iterator const_iter = const_cntr.begin() ;
  6. typename CNTR::reverse_iterator rev_iter = cntr.rbegin() ;
  7. typename CNTR::const_reverse_iterator const_rev_iter = const_cntr.rbegin() ;
  8. }

this code will not compile:
C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2.  
  3. void f( std::vector<int>::const_iterator& i )
  4. { std::cout << *i << std::endl; }
  5.  
  6. int main()
  7. {
  8. std::vector<int> v;
  9. std::vector<int>::iterator b = v.begin() ;
  10. f(b);
  11. }
but this will:
C++ Syntax (Toggle Plain Text)
  1. template< typename ITERATOR > void f( ITERATOR i )
  2. { std::cout << *i << std::endl; }
  3.  
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8. std::vector<int> v;
  9. std::vector<int>::iterator b = v.begin() ;
  10. f(b);
  11. }
c++09: containers also have:
C++ Syntax (Toggle Plain Text)
  1. // ...
  2. const_iterator cbegin() const;
  3. const_iterator cend () const;
  4. const_reverse_iterator crbegin() const;
  5. const_reverse_iterator crend () const;
  6. // ...
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.
Thanks everybody for discussion.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
brain is offline Offline
9 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: fopen
Next Thread in C++ Forum Timeline: How do I write code for PAC MAN!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC