-4

This is the code that I have written

I am getting an error when trying to print out the vector values and I can not figure out why. I passed the array to the function print(&v1). Can someone help me figure out why this error is occurring and if there is a better alternative way to print out the vector elements in my function.

D.Mensah
  • 27
  • 5
  • 5
    Please include both the code and the error as text in the question. Also please read about [mcve] – 463035818_is_not_an_ai Oct 02 '18 at 15:11
  • 1
    You have some misunderstanding about **pointers** and **references**. I think this is not a good place to try and teach that, you should probably pick up a [good book](https://stackoverflow.com/q/388242/1171191), or ask your teacher to explain those concepts again. – BoBTFish Oct 02 '18 at 15:11
  • The simple answer to your question is to change `v[x]` to `(*v)[x]` in `print`, but I don't think that will really help your understanding much. (And it is one fix, but probably not the best fix). – BoBTFish Oct 02 '18 at 15:16
  • 1
    `print1` seems to be correct. Why do you want to use a pointer in `print`? You need pointers a lot less often than some teachers are trying to make you believe. – 463035818_is_not_an_ai Oct 02 '18 at 15:17
  • @user463035818 sorry first time asking a question on stack overflow, quickly had to get the question out – D.Mensah Oct 02 '18 at 15:21
  • 1
    `vector *v` -> `vector & v` – Jabberwocky Oct 02 '18 at 15:21
  • @D.Mensah is copy pasting text really more difficult than copy pasting a screen shot? – Jabberwocky Oct 02 '18 at 15:22
  • "quickly had to get the question out " sorry but thats the wrong approach. You should have read the [help] and take the [tour] before asking the question. This would save you some downvotes ;) – 463035818_is_not_an_ai Oct 02 '18 at 15:22
  • @user463035818 the outline of the lab that I am working on right now requires that we use a pointer in print – D.Mensah Oct 02 '18 at 15:23
  • @D.Mensah I you really want a pointer in `print`, the 3rd comment is the answer. – Jabberwocky Oct 02 '18 at 15:25
  • then thats a stupid unrealistic requirement and they should have told you before the assignment how to work with pointers, otherwise I dont get the point of the exercise, aka you need to review your lecture notes ;). Dont get me wrong, but SO cannot replace classes, direct teaching or reading books – 463035818_is_not_an_ai Oct 02 '18 at 15:25
  • This is one of those problems where the issue is the _level of indirection_. Your compiler error is telling you that you're trying to print an entire vector, when you want to print a single member. That's because the parameter to the function is a pointer, rather than a reference. I understand that your instructor wants you to pass a pointer (perhaps to press home the lesson that they're really not the best solution, quite often) but the best fix might be, as BoBTFish suggested, simply dereference the pointer to get back to the vector, before using the array index to get the actual value. – Tim Randall Oct 02 '18 at 15:25

1 Answers1

4

You correctly used -> in print1 instead of . (like in print) to operate on the pointer to the vector. This works because x->y is equivalent to (*x).y, meaning you correctly dereferenced the pointer to the vector before accessing it.

Except for []. Here you also have to dereference the pointer before using []. So:

cout << (*v)[x] << endl;

There is no abbreviation (also called "syntactic sugar") for (*x)[] like there is for (*x).y, so you must do it manually.


The error message is confusing because using [] on a pointer is valid - x[y] is equivalent to *(x+y), which means you are doing pointer arithmetics: You use v as if it was a (C-style) array of vectors, and you try to get the xth element from this array of vectors. Lucky for you, the compiler doesn't know how to << a Vector with cout - but if it could, the code would compile and do something you (probably) did not intend.

Max Langhof
  • 23,383
  • 5
  • 39
  • 72