ken.carlino@gmail.com wrote:[color=blue][color=green]
>>From
http://www.parashift.com/c++-faq-lit...s.html#faq-8.6, it[/color]
> said "Use references when you can, and pointers when you have to."[/color]
That's to distinguish between pointers and reference. In your case, you
simply need an object. I understand, coming from Java some folks do not
grasp that concept sometimes. You need to make an effort.
[color=blue]
> And I need to convert this java class to c++:
>
> public class Y {
> public final int[] w
>
> public Y(final List alist {
> w = new int[alist.size()];
> }
> }
>
> So I am thinking about create a Reference of stl:vector for my
> attribute 'w'.
>
> class Y
> {
> public:
> Y
> virtual ~Y();
>
> vector<int>& w;[/color]
Should simply be
vector<int> w;
[color=blue]
> };
>
> and in my constructor, i initialize it like this:
> Y::Y(list<Z>& alist) :
> w ( vector<int>(alist.size()) )[/color]
Should instead be
w(alist.size())
[color=blue]
> {
>
> }
>
> And I get this compilation error:
> ../src/YMapData.cpp:9: error: invalid initialization of non-const
> reference of type 'std::vector<int, std::allocator<int> >&' from a
> temporary of type 'std::vector<int, std::allocator<int> >'
>
> I appreciate if someone can tell me what did I do wrong.
>[/color]
You will need to learn to _instantiate_ objects.
V