简体   繁体   English

无法将uint8_t向量迭代器转换为const uint8_t *

[英]Unable to convert a uint8_t vector iterator to a const uint8_t *

I have a vector object that contains clipboard data. 我有一个包含剪贴板数据的矢量对象。 I am trying to write the contents of the clipboard data into a temp file using a buffered stream. 我试图使用缓冲流将剪贴板数据的内容写入临时文件。 I am using iterators to access the contents of the vector. 我正在使用迭代器来访问向量的内容。

I am running into trouble while trying to convert the clipboard data which is a std::vector ClipboardDataVector to inbuffer which of type const std::uint8_t* inBuffer . 我在尝试将剪贴板数据( std::vector ClipboardDataVectorinbuffer时遇到了麻烦,类型为const std::uint8_t* inBuffer

Here is the code that I use 这是我使用的代码

typedef std::vector ClipboardDataVector;

File::WriteBlock(const std::uint8_t* inBuffer, std::uint32_t inBufferSize);

BOOL WriteToTempFile(ClipboardDataVector& clipBoardData) { 
  std::vector::iterator clipBoardIterator; 
  clipBoardIterator = clipBoardData.begin();
  File::WriteBlock((const uint8_t *)clipBoardIterator, clipBoardData.size());
}

When I compile this code I get the following error. 当我编译此代码时,我收到以下错误。

error C2440: 'type cast' : cannot convert from 'std::_Vector_iterator<_Myvec>' to 'const uint8_t *' 错误C2440:'type cast':无法从'std :: _ Vector_iterator <_Myvec>'转换为'const uint8_t *'

I am new to vectors and I am finding it hard to get my head around this error - how can I resolve it? 我是向量的新手,我发现很难理解这个错误 - 我该如何解决?

When you use a std::vector you need to specify the type it holds. 当您使用std::vector您需要指定它所拥有的类型。 So your typedef needs to be: 所以你的typedef需要是:

typedef std::vector<uint8_t> ClipboardDataVector;

Once you've done that if you have a vector of that type and you want to get a const uint8_t * the usual idiom is: 一旦你完成了这个,如果你有一个这种类型的向量,你想得到一个const uint8_t *通常的习惯用法是:

void WriteToTempFile(const ClipboardDataVector& clipBoardData) {
   const uint8_t *data = clipBoardData.size() ? &clipBoardData[0] : NULL;
   // ...
}

This works because vectors have contiguous storage - it's asking for a pointer to the first element in the vector. 这是有效的,因为向量具有连续的存储 - 它要求指向向量中的第一个元素。 It also checks for the special case where the vector is empty and so the subscript operator can't be used. 它还检查向量为空的特殊情况,因此不能使用下标运算符。 This clearly won't work for something like std::list where the elements aren't always contiguous. 这显然不适用于像std::list这样的元素并不总是连续的。

You were on the right sort of track with iterators, but iterators are a generalisation of the concept of a pointer - that is they look and feel like a pointer (by either being a pointer or some operator overloading) but they're not necessarily going to actually be a pointer always. 您使用迭代器处于正确的轨道上,但迭代器是指针概念的概括 - 它们看起来像一个指针(通过指针或某些运算符重载)但它们不一定会发生实际上总是一个指针。 If you need a pointer from a vector (because you're interacting with C usually) then the address of the first element is the safe, portable way to do it. 如果你需要一个来自向量的指针(因为你通常与C进行交互),那么第一个元素的地址就是安全,可移植的方式。

(I also made the reference to clipBoardData be const also - it's a good habit to be in, marking things you won't alter as const always) (我也将clipBoardData的引用也clipBoardData const - 这是一个很好的习惯,标记你不会像const那样改变的东西)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM