I have a question about vector
push_back()
.I am writing a code to acquire image into the buffer and store them inside a vector
.I checked that I may need to use push_back()
,But I am not sure right now how should I format pImageBuffer
that I can store it into the vector
.Maybe it is another easy question,but I have been trying a whole morning but still confused.I hope someone can give me some light.Thanks very much in advance!!
the Result.Buffer() is from a camera SDK,it will renew by StreamGrabber.QueueBuffer.My problem is whenever I build I got error like :
std::vector<_Ty>::push_back': Konvertierung des Parameters 1 von 'const uint8_t *' in 'unsigned char *const &' not possible
My code is some how like this. ....
std::vector<uint8_t *> images(100);// here is the problem ,it needs a "const",or delete const down const uint8_t *pImageBuffe.Thanks very much guys 开发者_StackOverflow社区for your answers!!!
while(1){
....
const uint8_t *pImageBuffer = (uint8_t *) Result.Buffer();// Here I get the image data
images.push_back(pImageBuffer);// This is obvious wrong,but I am not sure in side (),what should I do
StreamGrabber.QueueBuffer(Result.Handle(), NULL);
...
}
You have a const uint8_t*
, but your container stores uint8_t*
.
Also note that you're adding a 100th, 101st, 102nd element there, not filling the 0th, 1st, 2nd...
You may want:
std::vector<const uint8_t*> images(100);
size_t i = 0;
while (i < 100) {
// ..
const uint8_t* pImageBuffer = (uint8_t*)Result.Buffer();
images[i++] = pImageBuffer;
// ..
}
Or you may want:
std::vector<const uint8_t*> images;
while (true) {
// ..
const uint8_t* pImageBuffer = (uint8_t*)Result.Buffer();
images.push_back(pImageBuffer);
// ..
}
Or you may want combinations without the const
.
Remove the const
:
std::vector<uint8_t *> images;//(100);
while(whateverCondition)
{
uint8_t *pImageBuffer = (uint8_t *) Result.Buffer();
images.push_back(pImageBuffer);
}
Make sure Buffer()
function allocates new buffer everytime its called.
The type of the vector elements needs to be the same as the type of the thing you are putting inside it (or at least compatible).
I suspect what is wrong is that you are pushing a value of type const uint8_t *
into a vector whose elements have type uint8_t *
-- that is, you are trying to convert a const pointer into a non-const pointer.
Change the vector's type to std::vector<const uint8_t *>
.
In your code example, the only thing which is really wrong is that you're trying to put a const uint8_t *
into a vector of uint8_t *
. Try removing the const
and see whether that works better.
You don't need the (100)
- and you need to be consistent about const
.
精彩评论