0

I am trying to create a vector as show below:

std::vector<double> dimensions_data_vec{input_shape_pointer.get_dimensions()};

In this code, input_shape_pointer is a pointer to a shape such as a rectangle. A shape has dimensions associated with it, eg. length and width. I now have to create another, separate class which takes a pointer to a shape and accesses its dimensions. To do this I am using the code snippet.

The get_dimensions() function is a part of the shape class and returns the dimensions of a shape in a vector which has a type double.

My code issues the error

Expression must have class type but it has type "*shape" (please note the asterisk here)

My question is, how do I have get_dimensions() work on the shape before the vector is initialised so that there is no mismatch, and dimensions_data_vec just takes in the vector double of shape dimensions? I think there may be an issue with initializing a vector with another vector anyway, but I want to work on one problem at a time.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Does this answer your question? "[What can I use instead of the arrow operator, `->`?](https://stackoverflow.com/q/221346/90527)", "[What is the difference between the dot (.) operator and -> in C++?](//stackoverflow.com/questions/1238613/90527)" – outis Apr 08 '22 at 03:22

1 Answers1

0

If input_shape_pointer is a pointer to a shape then use -> instead of .. For example, the expression

input_shape_pointer.get_dimensions()

should be replaced by:

input_shape_pointer->get_dimensions()

Note the use of -> instead of . in the above expression

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jason
  • 36,170
  • 5
  • 26
  • 60
  • Thank you very much, my error has now gone. However, I am now getting a segmentation fault with get_dimensions which looks like. std::vector/get_dimensions(shape shape_ptr){ std::vector dim_vec; for(int i{0}; i<2; i++){ dim_vec.push_back(dimensions_data[i]); } return dim_vec; } where dimensions_data contains the dimensions in a double array. – purecobalt Apr 08 '22 at 03:20
  • @purecobalt Make sure that the pointer `input_shape_pointer` is not uninitialized before dereferencing it. That is, make sure that the pointer is pointing to some `shape` object before dereferencing it. Otherwise, you'll have undefined behavior which may result in a crash. – Jason Apr 08 '22 at 03:24
  • I've double checked this but haven't dereferenced it. I think the seg fault is from dimensions_data[i] but can't see why this would be the case as I initialise that array with size 3. – purecobalt Apr 08 '22 at 03:33
  • @purecobalt You've dereferenced the pointer `input_shape_pointer` when you wrote: `input_shape_pointer->get_dimensions()`. So before writing the expression `input_shape_pointer->get_dimensions()` make sure(if not already) that `input_shape_pointer` is pointing to some `shape` object. If this still doesn't solve the problem, you can ask your follow up question as a separate question with sufficient details given in that question. – Jason Apr 08 '22 at 03:36
  • Please may you explain how I go about doing this? My only idea is to do it in the main code. – purecobalt Apr 08 '22 at 03:39
  • @purecobalt For example, you could do something like: `shape s; s.setDimensions(5,6); shape* input_shape_pointer = &s; std::vector dimensions_data_vec{input_shape_pointer->get_dimensions()};`. Since you've not given enough information in the question for us to debug, we can't say exactly where the problem is. In the above given snippet, i have made sure that `input_shape_pointer` points to object `s`. Similarly, you also have to make sure the same. – Jason Apr 08 '22 at 03:41