Skip to main content
gamedev.net
🔒 Locked

Problem with const argument....

Started by devronious Apr 9, 2009 at 10:03 AM 5 replies 950+ views
Original Post
devronious
devronious
Hey I have a native struct and method and I'm wrapping it in CLI and when I create the wrapper to this function I get an argument: error C2665: 'sqrt' : none of the 3 overloads could convert all the argument types Here is the native method:
[source lang = "cpp"]
qd_real sqrt(const qd_real &a) {
  /* Strategy:  

     Perform the following Newton iteration:

       x' = x + (1 - a * x^2) * x / 2;
       
     which converges to 1/sqrt(a), starting with the
     double precision approximation to 1/sqrt(a).
     Since Newton's iteration more or less doubles the
     number of correct digits, we only need to perform it 
     twice.
  */

  if (a.is_zero())
    return 0.0;

  if (a.is_negative()) {
    qd_real::error("(qd_real::sqrt): Negative argument.");
    return qd_real::_nan;
  }

  qd_real r = (1.0 / std::sqrt(a[0]));
  qd_real h = mul_pwr2(a, 0.5);

  r += ((0.5 - h * sqr(r)) * r);
  r += ((0.5 - h * sqr(r)) * r);
  r += ((0.5 - h * sqr(r)) * r);

  r *= a;
  return r;
}


and here is the CLI method:
[source lang = "cpp"]
			static QuadReal Sqrt(QuadReal n)
			{
				qd_real qr = std::sqrt(n.value);
				return QuadReal(qr);
			}


the value member in "QuadReal" is of type qd_real*. Not sure whats going on. Thanks in Advance, Devin
SiCrane
SiCrane
The qd_real sqrt() function doesn't live in namespace std. Take off the std:: from the sqrt() call.
devronious
devronious
Thanks, but now I get this:

error C2665: 'sqrt' : none of the 5 overloads could convert all the argument types
devronious
devronious
Hey it says its in the Global Functions and Variables. Is that the problem?
NotAYakk
NotAYakk
You are passing a pointer to a function that expects a reference.

Why do you think that would work?
devronious
devronious
does the &a in the argument mean reference? Cause yes I'm passing a pointer to a native struct. And the struct is native so shouldn't it be a pointer anyways?
NotAYakk
NotAYakk
& is a reference. A reference is not a pointer or a managed pointer.
* is a pointer. A pointer is not a reference or a managed pointer.
^ is a managed pointer. A managed pointer is not a reference or a pointer.

References are aliases to a variable. When you construct them, you say what they are an alias do -- after that, they behave much like they where the thing you constructed them from. It is like saying "this is another name for that".

Pointers are analogous to a 'street address' of a variable written down on a card. You can change what address is written on the card. You can use it to go to the address, and deal with the thign at the address.

Managed Pointers are like a registration number at the central housing office with a GPS link that the central housing office can track. The central housing office keeps track of who has an outstanding registration number. They are heavier, more expensive, and can do seemingly crazy things.

Your problem is you have a function that takes a const reference (an alias that is not allowed to change what it is an alias for), and are passing it a pointer. These are not the same type. So your compiler says "cannot find a match".

You can turn a pointer variable that is non-null into an alias by using unary operator*.

And you really should be posting these questions in 'for beginners'.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.