Ambiguous call again

  • Thread starter Thread starter Anna Smidt
  • Start date Start date
A

Anna Smidt

I am having an "ambiguous call to overloaded function" error again.

This is the function:

int nGetProfWidth (int ncols, unsigned ProfSpec)
{
if ((ProfSpec & PROF_2d) == 0)
return ncols;
return int(sqrt(ncols)); //HERE THE ERROR IS THROWN
}

When I want to have an integer returned (as in the code above - at least
I think it should return an integer), how should I rewrite this code?

Anna
 
Anna said:
I am having an "ambiguous call to overloaded function" error again.

This is the function:

int nGetProfWidth (int ncols, unsigned ProfSpec)
{
if ((ProfSpec & PROF_2d) == 0)
return ncols;
return int(sqrt(ncols)); //HERE THE ERROR IS THROWN
}

When I want to have an integer returned (as in the code above - at least
I think it should return an integer), how should I rewrite this code?

Anna:

return int(sqrt(double(ncols)));

You do know that most integers do not have an exact integer square root?
 
You do know that most integers do not have an exact integer square root?

Thanks and...
No, I did not know that. But I was wrong in the first place already
anyway because I was thinking of a VB6 integer which isn't equal to
VC++ integers as far as I know. I'm already tired enough to drop on the
table, so I will look it up tomorrow morning (yes, I have a C++ book :-)

Anna
 
Oh, I have a question before I go to bed:

double hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) );

Why is there a space after "sqrt(" and before the last ")"?
 
Okay, 1 last question:

MatView Mat::viewAsSquare ()
{
size_t ncols = size_t(sqrt(this->nelems()));
// check that can square properly (assertion is not essential but
protects the user)
ASSERT(ncols * ncols == this->nelems());
return MatView(*this, 0, 0, ncols, ncols, ncols);
}

The compiler tells me

error C2514: 'size_t' : class has no constructors

Why? Sorry for my many questions...
Anna
 
Anna said:
Thanks and...
No, I did not know that. But I was wrong in the first place already
anyway because I was thinking of a VB6 integer which isn't equal to VC++
integers as far as I know. I'm already tired enough to drop on the
table, so I will look it up tomorrow morning (yes, I have a C++ book :-)

Anna:

This has nothing to do with computer languages; it's just math. What is the
integer square root of 20? According to your formula it is 4. This may be the
answer you want; only you know that.
 
Anna said:
Oh, I have a question before I go to bed:

double hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) );

Why is there a space after "sqrt(" and before the last ")"?

Anna:

White space (of this kind) is ignored in C and C++ expressions.
 
Anna said:
Okay, 1 last question:

MatView Mat::viewAsSquare ()
{
size_t ncols = size_t(sqrt(this->nelems()));
// check that can square properly (assertion is not essential but
protects the user)
ASSERT(ncols * ncols == this->nelems());
return MatView(*this, 0, 0, ncols, ncols, ncols);
}

The compiler tells me

error C2514: 'size_t' : class has no constructors

Anna:

I cannot reproduce this on VS2008. What compiler version are you using?
 
Anna Smidt said:
Oh, I have a question before I go to bed:

double hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) );

Why is there a space after "sqrt(" and before the last ")"?

For readability of code.

Giovanni
 
Anna Smidt said:
Okay, 1 last question:

MatView Mat::viewAsSquare ()
{
size_t ncols = size_t(sqrt(this->nelems()));

Try:

size_t ncols = static_cast< size_t >( sqrt( this->nelems() ) );

Giovanni
 
Anna said:
Where do I find this info?

Anna:

I really meant just what version of Visual Studio are you using. If you really
do not know, select "About Microsoft Visual Studio" (or some such) from the Help
menu.

Could you provide a complete example illustrating this problem?

Something like

#include <math.h>

int main()
{
int n = 100;
size_t ncols = size_t(sqrt(double(n)));
ncols; // prevent unused variable warning
return 0;
}

This code compiles fine for me on VS2008. Does it for you?
 
Anna said:
Oh, I have a question before I go to bed:

double hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) );

Why is there a space after "sqrt(" and before the last ")"?

Because some people believe that it makes to code easier to read.

They are wrong. :-)


And it confuses beginners...


Bo Persson
 
Yes, it works for me.

But I don't know what else I could provide but the code below.
As far as I understand it, MatView Mat::viewAsSquare() requires code
from "all.hpp", right?
Should I post the code from "all.hpp"?
Anna

#include "all.hpp"

static const bool USE_EQ_DOUBLES = true; // 1 to for match within
tolerance
static const char *MAT_PRINT_FORMAT = "%8.4f "; // default mat print format

namespace GslMat

//-----------------------------------------------------------------------------
MatView Mat::viewAsSquare ()
{
// size_t ncols = size_t(sqrt(this->nelems()));
size_t ncols = static_cast< size_t >( sqrt( this->nelems() ) );
// check that can square properly (assertion is not essential but
protects the user)
ASSERT(ncols * ncols == this->nelems());
return MatView(*this, 0, 0, ncols, ncols, ncols);
}
 
Anna said:
Oh, sorry, VC2008 here.

Anna:

And what about a complete compilable example?

When you provide a sample that depends on stuff not present in the sample, then
it is not possible to test your code as given, so the responder has to create a
compilable example which might (for some unknown reason) not exhibit the problem.

If you create the self-contained example yourself, and post that, then everybody
can be on the same page. But very often, the exercise of creating a
self-contained example will allow you to solve the problem for yourself.
 
Anna said:
Yes, it works for me.

But I don't know what else I could provide but the code below.
As far as I understand it, MatView Mat::viewAsSquare() requires code
from "all.hpp", right?
Should I post the code from "all.hpp"?
Anna

#include "all.hpp"

static const bool USE_EQ_DOUBLES = true; // 1 to for match within
tolerance
static const char *MAT_PRINT_FORMAT = "%8.4f "; // default mat print
format

namespace GslMat

//-----------------------------------------------------------------------------

MatView Mat::viewAsSquare ()
{
// size_t ncols = size_t(sqrt(this->nelems()));
size_t ncols = static_cast< size_t >( sqrt( this->nelems() ) );
// check that can square properly (assertion is not essential but
protects the user)
ASSERT(ncols * ncols == this->nelems());
return MatView(*this, 0, 0, ncols, ncols, ncols);
}

Anna:

If my simpler example works for you, the it is really up to *you* to figure out
what is between the simple example and your code. We cannot do that because we
do not have the code.

But since your code is at namespace and class scope, perhaps you have done
something to redefine size_t in this scope.

What happens if you do

::size_t ncols = static_cast< ::size_t >( sqrt( this->nelems() ) );

?

By the way, why are you not getting the ambiguous call on the sqrt here? What is
the type of this->nelems?
 
Thank god, the constructor error is gone!! Thanks very much!
By the way, why are you not getting the ambiguous call on the sqrt here?
I do get the ambiguous call error in the line you suggested:
Error 6 error C2668: 'sqrt' : ambiguous call to overloaded function
cmat.cpp 1101 ASMModel

Now this is one of the last bugs remaining.
What is the type of this->nelems?
I don't know, the code is so huge and I am so newbie to C++ that I have
not yet been able to figure out where nelems originates from.

Anna
 
Back
Top