I have a problem in this code snippet, it is giving the error call to 'abs' is ambiguous.
for (NSUInteger idx = 0; idx < count; idx++) {
if ((std::abs(toValues[idx] - previousValues[idx]) >= t) || (std::abs(previous2Values[idx] - previousValues[idx]) >= t)) {
return false;
}
return true;
}
There are two possible causes for call to some_function() is ambiguous.
Arguments in a function call do not match those in any function
declaration.
I hope the arrays toValues and previousValues are of the same type. In this case check if the value toValues[idx] - previousValues[idx] is of a type that is allowed by abs() as an argument. According to C++11 standard the allowed values are int, long int and long long int.
Perhaps you could cast the value of the toValues[idx] - previousValues[idx] to integer or another permitted type to rule out this as a cause like below :
(int)toValues[idx] - previousValues[idx]
The same function is defined more than once
abs() is a massively overloaded function. You could check if it is included multiple times in your code using multiple header files. For example inclusion of math.h and cmath.h will result in a duplicate abs() function which will lead to ambiguity. However, such inclusion of headers does not make any sense. Rather such inclusion is accidental.
Related
I have this code, which is giving me a single error that indicates two problems.
int healthyConst = 0;
int sickConst = 1;
int recoveredConst = 2;
GraphMatrix<int, double> graph (100);
for (int i = 0; i < sampleSize; i++)
{
if(std::rand() % 2 > 0.05) graph.setVertexInfo(i, sickConst); //Error
else graph.setVertexInfo(i, healthyConst);
}
The error is:
error: no matching function for call to GraphMatrix::setVertexInfo(int&, int*)
And the function in question is declared as follows in the source:
void GraphMatrix::setVertexInfo(int v, VertexObject& info)
First, i should not be a reference. This seems nonsensical to me, yet I can not fix this. If I try to outsmart the compiler and type for(int* i = 0...) the error now complains of setVertexInfo(int&*, int*), and I don't even understand what this means.
Second, sickConst is not a pointer. It is just an int. Now I realize the method, as written, accepts VertexObject&, not VertexObject, but *sickConst also causes the compiler to complain of invalid type argument of 'unary *'. I've also tried &sickConst, which the compiler not unexpectedly interprets as a pointer.
Also note, identical errors are thrown for the second line of the for loop, presumably for the same reasons.
The question is: why am I getting these errors, and how do I fix them?
You stated that your function declaration within the source is as follows:
void GraphMatrix::setVertexInfo(int v, VertexObject& info)
However in your for loop you are passing it a type of int. Either change your function declaration & definition to accept a type of int or change the type that you are passing to your function as a VertexObject.
int main(){
int sum_digits(int tiv) {
int sum = 0;
while (tiv > 0) {
sum += tiv % 10;
tiv /= 10;
};
return sum;
};
int num_root(int num) {
int root = sum_digit(int num);
while(root > 9) {
sum_digits(int root);
};
return root;
};
return 0;
}
This is the code that I have written the first function calculates the sum of the digis of a number. The second function uses a loop to calculate the sum of the digits repeatedly until a one digit numer is obtained (i.e. the numeric root).
But when I compile the code, this is what I get
5:29: error: a function-definition is not allowed here before '{' token (the first line of main)
22:2: error: expected '}' at end of input (the last line)
Some guidance would be appreciated, thanks in advance.
As people have indicated in the comments, you shouldn't define a function inside of another function like this.
int main(){
// This is defined inside of main
int sum_digits(int tiv) {
int sum = 0;
while (tiv > 0) {
I'd always encourage proper code indentation as that would have made this problem much more obvious.
Also, this isn't the correct way to do a function call:
int root = sum_digit(int num);
You don't need to include the int again here (in fact, it's not syntactically valid to do so) - it's already clear from the rest of your code that num is an int.
Same thing here:
while(root > 9) {
sum_digits(int root);
};
Also, you don't actually do anything with the return value of sum_digits, so this will literally do nothing. It will either be an infinite loop (if root is greater than 9), since the value of root never changes, or the loop won't ever run (if root is less than 9).
One final point: in order to have this program actually do anything, you'll have to call the functions from your main method with some value. Right now even if you fix the compilation errors the program won't work because you don't actually determine what values you want the root of and you never actually call the functions you wrote.
defining functions within functions is nothing you should do in c/c++. I think gcc supports this, but afaik its not standard conforming.
just declare and define your functions not within other functions. use anonymous namespaces if you want to hide them from your public api
I had this code as part of a C++ project
unsigned int fn() {
//do some computations
int value = ....
if(value <= 0)
return 0;
return (unsigned int)value;
}
I don't get the need to use the cast at the last return statement since all negative numbers will be caught in the if statement(hence returning 0).
And moreover this function fn is called from another function (whose return type is int) that simply returns the value returned by fn.
Thanks..
The purpose is to silence the compiler warning that could otherwise be issued.
I think that it really changes nothing
I personally run the same code for different scenarios and it appears the last cast can be done away with.
I have a simple couple lines of code
std::regex_iterator<std::string::const_iterator>
regit (attributesStart, _curIter, _attributeRegex),
regend(std::regex_iterator<std::string::const_iterator>);
while (regit != regend)
{
// [...]
}
The compiler complains about the while line, saying
Invalid operands to binary expression ('std::regex_iterator' and 'std::regex_iterator (*)(std::regex_iterator)')
Any idea why this is?
int main()
{
int i = 0, f(int);
f(i);
}
int f(int) {return 0;}
Is valid code. There is a block-scope function declaration of f inside main, in the same init-declarator-list as i, which is a usual int.
The same happens in your case, just in a more complicated manner. Here the function declared is regend and has a parameter of type std::regex_iterator<..> - you could have also deduced this by inspecting the error message. The problem wouldn't be solved by using std::regex_iterator<std::string::const_iterator>(); Then the parameter would be a pointer-to-function instead, but regend not a variable. Use uniform initialization (i.e. { and }) or double braces to avoid this.
I'm getting an error which says that two overloads have similar conversions. I tried too many things but none helped.
Here is that piece of code
CString GetInput(int numberOfInput, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT);
CString GetInput(int numberOfInput, string szTerminationPattern, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT);
I can't understand how could string be equal to long?
I'm using Visual C++ 6 (yep I know its old, I'm working on legacy code, so I'm pretty much helpless)
EDIT: The line of code that is triggering the error is
l_szOption = GetInput(13, FALSE, 30 * 10);
The problem is caused by the fact that you are supplying the timeout argument as a signed integer value, which has to be converted to an unsigned one for the first version of the function (since the timeout parameter is declared as UINT).
I.e. the first version of the function requires a conversion for the third argument, while the second version of the function requires a conversion for the second argument (FALSE, which is just 0, to string). In this case neither function is better than the other and overload resolution fails.
Try explicitly giving the third argument the unsigned type
l_szOption = GetInput(13, FALSE, 30U * 10);
or
l_szOption = GetInput(13, FALSE, (UINT) 30 * 10);
(whichever you prefer) and the code should compile as expected.
In other words, the compiler is absolutely right to complain about your code. Your code is indeed broken. The problem in your code has exacty the same nature as in the following simple example
void foo(int i, unsigned j);
void foo(unsigned i, int j);
int main() {
foo(0, 0);
}
This code will also fail to compile for precisely the same reason.
GetInput(13, FALSE, 30 * 10);
My guess is that
FALSE ==> o ==> NULL is getting converted to std::string(NULL)
hence, it cannot determine which method to instantiate.
T0 prove this check this :
GetInput(13, TRUE, 30 * 10); //it works
You are possibly passing that function a second parameter that is neither a BOOL, nor a string, but a type that could be implicitly converted to either.
A character pointer, for example.
To resolve the ambiguity when you call the function either cast the second parameter to BOOL or use string("whatever") if that is indeed std::string.
Consider following case :
BOOL is typedef of int.
GetString(10,'a'); // compiler get confused in resolving the function
whether 'a' is to be converted to BOOL or string ???
When you make function call give proper argument by using static_cast to make desired function to call.
char ch = 'a';
GetString(10,static_cast<BOOL>(ch)); // calls function with 2nd argument as BOOL
GetString(10,static_cast<string>(ch)); //calls function with 2nd argument as string