I have been trying to find an answer and get this to work for a while now and so I figured I’d check here. I am trying to pass a pointer to a long by reference. The syntax I’m using is:
int Hex2Loong(long*& bytebuff, int& len, const std::string Hexbuff);
The error shows up whenever I try to declare the function. The definition itself surprisingly
seems to have no issues. I’m using the same exact syntax with a int*& in my Hex2Int fcn
and there are no problems there. Any ideas what I’m messing up?
The error I’m getting is:
error:in passing argument 1 of Hex2Loong(long int*&,int &,const std::string);
On a related note if I have a unsigned long * I take it there is no way to cast that so I
can pass it into the Hex2Loong function right? I will have to create temporary long *
vars cast into them and pass those?
Ty for any answers,
Drf
Edit:
Hmm ok so to answer some of the comments.
I pass a reference since the function converts a possibly very long hexadecimal string
to an array of longs. (i.e. think a number on the order of 2^1000 written as a hex string)
and I need to pass the number back as an array of longs (I will probably rewrite it for
unsigned longs since the no casting makes it cumbersome).
The HexString is declared as const since it’s supposed to be (it should not be modified only read).
And finally you were quite right the error had nothing to do with the declaration but
rather with the usage. The fact that I was trying to pass (long *) num1 where num1
was unsigned long * was messing everything up. I still don’t get why the error showed
up in the header file though instead of just the place where I was actually calling
the function with the wrong parameters.
Edit2: Ooh I just realized what you meant by passing const std::string&. And of course
you’re exactly right. I will change that aswell. It’s what I get for writing in java
too much. I didn’t realize the string would actually get copied.
You should be able to call that function fine with something like:
What you can’t do is pass the address of the long, since you need a real reference. In other words, don’t try:
See the following sample code:
This compiles okay but complains when you uncomment the
&lvarline.In terms of your second question, no, you need an actual variable that can be passed as a reference, simply because the function may attempt to change it. So you’ll need to make a temporary copy somewhere.
However, keep in mind that, unless you actually need to change the value of the variable you’re passing in (so that such a change is reflected in the calling code), there’s no need to pass a reference. You can just pass it by value and use the value within the function.
If you do that (ie, if you don’t need to change it), you can then use temporaries like
&lvaror(long*)(unsigned_long_ptr_variable)in the function call.