-1
addWidget(const Widget& nWidget){
Widgets.push_back(nWidget);

i am trying to send an object from a x* by ref in to a vector holding and i get the following error

    1   IntelliSense: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=Widget *, _Alloc=std::allocator<Widget *>]" matches the argument list
        argument types are: (const Widget *)
        object type is: std::vector<Widget *, std::allocator<Widget *>>

what am i missing here?

SoNiC306
  • 5
  • 5

1 Answers1

0

Based on the error message it appears that Widgets is an std::vector<Widget *>. If that is the case, then you could make the code work simply by taking a pointer to nWidget:

Widgets.push_back(&nWidget);

As well as by changing Widgets to std::vector<Widget const *>, or changing the argument to Widget & (remove the const) -- taking a pointer to a Widget const & results in a Widget const * so you would get an error that you are discarding const qualifiers.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • the name of the func must be "addWidget(const Widget& nWidget)" we are not allowed to change it i tryed to use "Widgets.push_back(&nWidget);" but i still get an error apparently adding vector < CONST widget*> solved the error, but in this case i wont be able to change any values in the widget it self one its stored in an array or am i mistaken? – SoNiC306 Sep 11 '14 at 17:05
  • @SoNiC306 Because the `const`-ness is different. If you can't change the signature of the function then you must change the vector to `std::vector`. – cdhowie Sep 11 '14 at 17:07
  • Thank you for the answer just one more thing ... what is the difference from "const widgets *" to "widget const *" - its blurry to me – SoNiC306 Sep 11 '14 at 17:10
  • @SoNiC306 [They mean the same thing.](http://stackoverflow.com/q/3694630/501250) I prefer `T const *` over `const T *` because in C++ you read from right to left, and `T const *` is more consistent. For example, `T const * const` is read right to left as "constant pointer to constant T." `const T * const` muddies the waters a bit: "constant pointer to a T... that is constant." If you always put `const` immediately following the thing you want to be constant, there is never any potential ambiguity. – cdhowie Sep 11 '14 at 17:20