Warnings with std::vector

Apr 24, 2012 at 9:25pm
Hi there,

I've got some warning when I try to use vectors..

Here is the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//*******
//test.h
#include <iostream>
#include <vector>
using namespace std;

typedef struct
{
   float test1;
   int test2; 
}
T_Test;

//*******
//test.cpp
#include "test.h"
void AddToVect()
{
   vector<T_Test> vect;
   T_Test example;
   vect.push_back(example);
}


I've got this warning for the line underlined :

warning: cast from
`const std::_Vector_iterator<T_Test, std::allocator<T_Test> >* const' to
`std::_Vector_const_iterator<T_Test, std::allocator<T_Test> >*' discards
qualifiers from pointer target type

Why do I have this warning with vectors of struct or Class ?

Thanks for your answers !

Anomen.
Last edited on Apr 25, 2012 at 6:40am
Apr 24, 2012 at 9:31pm
I think that something is missed in your example. But I see that two members of your structure have the same name. Please show the exact example of your code that generates the warning.
Apr 24, 2012 at 9:44pm
Thanks for your fast answer !

I edited my first post, this is what I tested (very simple case, my code is long so I didn't posted it here) and I got this warning ...
Can you see anything ?
Last edited on Apr 24, 2012 at 9:50pm
Apr 24, 2012 at 9:52pm
That is still not the complete code. It doesn't make sense that you get the warning you say you get from that code.
Apr 24, 2012 at 9:52pm
The warning message has nothing common with the code you show. Please show the exact code that generates the warning meassage.
Apr 25, 2012 at 6:56am
ay, I, understand.. I edited my code one more time. Here is the code, I DO have the warning with it .
Can you see anything?
Thanks a lot

Anomen
Last edited on Apr 25, 2012 at 6:57am
Apr 25, 2012 at 9:16am
No warnings here.
Apr 25, 2012 at 9:29am
I do not see your updated code. As I said early the presented code has nothing common with the warning.
Apr 25, 2012 at 11:55am
Vlad --> In my first post, you have what I tested and i've got the warning, the .h and .cpp
Peter87 -->Did you compile it ? Did you have the warning ? If not, do you have any Idee of where could the problem come from ? the compiler maybe ?

And thanks for the answers
Apr 25, 2012 at 12:05pm
I don't have a compiler here at the moment so I compiled it on ideone.com without any warnings. You shouldn't get that warning from that code. Are you sure you get a warning when you compile exactly that code? Sometimes people make the mistake and think they compile a file but instead compile some other file. Make sure you compile the correct files. If you use an IDE, create a new project, add the files to the project, compile and see if that changes anything.
Last edited on Apr 25, 2012 at 12:08pm
Apr 25, 2012 at 12:14pm
I compiled it with gcc 3.3.1..
In the repertory, I only have test.h and test.cpp and I get a test.o

Thanks for your answer !
Apr 25, 2012 at 1:35pm
Compiles fine 100% warning free with exact copy and paste of this code.
Apr 25, 2012 at 2:58pm
My bad, I missed a (big) part of the warning, here it goes :

/opt/qnx630/current/target/qnx6/usr/include/cpp/xlocale: In function `const
_Facet& std::use_facet(const std::locale&) [with _Facet = std::ctype<char>]
':
/opt/qnx630/current/target/qnx6/usr/include/cpp/xlocale:1339: instantiated from here
/opt/qnx630/current/target/qnx6/usr/include/cpp/xlocale:338: warning: cast from
`const std::locale::facet*' to `std::ctype<char>*' discards qualifiers from
pointer target type
/opt/qnx630/current/target/qnx6/usr/include/cpp/vector: In member function `
typename _Alloc::difference_type std::_Vector_iterator<_Ty,
_Alloc>::operator-(const std::_Vector_const_iterator<_Ty, _Alloc>&) const
[with _Ty = T_Test, _Alloc = std::allocator<T_Test>]':
/opt/qnx630/current/target/qnx6/usr/include/cpp/vector:760: instantiated from `std::_Vector
_iterator<_Ty, typename std::_Vector_val<_Ty, _Ax>::_Alty> std::vector<_Ty, _Ax>::insert(std:
:_Vector_iterator<_Ty, typename std::_Vector_val<_Ty, _Ax>::_Alty>, const _Ty&) [with _Ty = T
_Test, _Ax = std::allocator<T_Test>]'
/opt/qnx630/current/target/qnx6/usr/include/cpp/vector:707: instantiated from `void std::ve
ctor<_Ty, _Ax>::push_back(const _Ty&) [with _Ty = T_Test, _Ax = std::allocator<T_Test>]'
test.cpp:7: instantiated from here
/opt/qnx630/current/target/qnx6/usr/include/cpp/vector:291: warning: cast from
`const std::_Vector_iterator<T_Test, std::allocator<T_Test> >* const' to `
std::_Vector_const_iterator<T_Test, std::allocator<T_Test> >*' discards
qualifiers from pointer target type
Apr 25, 2012 at 3:05pm
Yeah you are leaving out a lot of code and it is hard to diagnose, but it looks like you are doing something inside a const member function (through some chain of calls or a const object), and you are modifying what you have promised not to modify. You will have to track it down, or you can post some actual code that is relevant.
Apr 25, 2012 at 3:05pm
To echo what everyone else has been saying, the code you posted does not generate that warning. You need to post the code that does. You apparently are leaving out the most important code.
Apr 25, 2012 at 3:13pm
The warning comes from inside the standard library files so maybe the implementation is to blame? You could try upgrading to a more recent GCC version.
Last edited on Apr 25, 2012 at 3:14pm
Apr 25, 2012 at 4:02pm
post the whole .h and .cpp, so we can see exatly where the error happens! Give us the WHOLE code!
Apr 25, 2012 at 4:54pm
The warning comes from inside the standard library files so maybe the implementation is to blame?


This is extremely unlikely. If vector::push_back really did have this issue it would have been found and fixed immediately.

It's much more likey that the OP is just not showing us what he's really doing, and is only posting what he thinks is the problem.
Apr 25, 2012 at 9:35pm
First of all thank you for your answers.
Secondly, I SWEAR you that I've got this warning with THIS code, I have no reason to lie or hide something..
I compiled this example to show you that it does not come from a complex code but just from that very simple example. And everytime, I use vectors, I got this warnings, whatever the code behind.

clanmjc ---> I will try to look ine the libraries to find what could be wrong
Peter ---> I would love to upgrade to the newest gcc, but I compile for QNX 6.3 and this is the best version I can have (maybe I could upgrade to 3.3.5)

Sorry for my english, and one more time I thank you for your effort !

Can you see something else or should I give up and use vectors with this warnings ?
Last edited on Apr 25, 2012 at 9:39pm
Topic archived. No new replies allowed.