0

so I am having some issues with creating and using pointers for vectors. The problem I'm trying to solve with these pointers, is referencing data, without having an excess amount of code. This is how I'm currently defining the variables:

// Data vectors
std::vector<int16_t> amountData;
std::vector<float> speedData;

std::vector<int16_t> *pointerr = &amountData; // Should be auto, just testing

I reference the data used multiple times through the code, which is why it would be easier if I could just have a pointer for the active data (data which I intend to use). I can't get it to work though, with using commands such as "*pointerr.size();" and such. I get the error:

request for member 'size' in 'pointerr', which is of pointer type 'std::vector<short int>*' (maybe you meant to use '->' ?)

and when using '*pointerr->size();', I get:

invalid type argument of unary '*' (have 'std::vector<short int>::size_type {aka long long unsigned int}')

I know its probably just me not fully understanding pointers/vectors, and that I'm probably missing something. Most of the other simallar questions don't really answer my problem (as far as I understand). I appreciate any sort of help/ideas and such, thanks in advance:)

5
  • @NateEldredge make that an answer. Commented Jun 28, 2022 at 15:46
  • What is the intention behind the two std::vector declarations with different types? You won't be able to have one pointer refer to one of those sometimes and the other at other times. The type of the pointer must statically match the vector type. Commented Jun 28, 2022 at 15:48
  • Taking the compilers error message too literally. Commented Jun 28, 2022 at 15:49
  • @user17732522 Ye, I know. It's for easier troubleshooting, so that I don't have to change a lot of code when I want to switch between datasets Commented Jun 28, 2022 at 16:00
  • @BjarkeLund If you have no intention of changing what pointerr refers to at runtime, then a reference instead of a pointer would be much easier to handle: auto& pointerr = amountData; and now you can use pointerr exactly as you would amountData (with very minor changes e.g. in decltype(pointerr)). Commented Jun 28, 2022 at 16:03

1 Answer 1

3

You want pointerr->size() (without a *); the -> operator does the dereference of pointerr for you.

Or alternatively, (*pointerr).size() which is equivalent. Your attempt of *pointerr.size() was close, but the . operator has higher precedence than *, and you have to derefence the pointer before you can apply . to the object it points to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.