Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I keep getting a syntax error when trying to compile the following:
template <class Type>
Type larger(Type x, Type y)
{
return (x >= y) ? x : y);
}
I am used to the normal if-else statements but I was trying to get some practice with this format. Any idea where I went wrong?
You have an extra ) at the end of the return statement :
return (x >= y) ? x : y);
should be :
return (x >= y) ? x : y;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
double recursively(int n) {
(n==0)?(return 0.0):((n==1)?(return 1.0):(return 2*recursively(n-1)+recursively(n-2)));
}
When I use the ternary operator in a recursive function it is not showing "expected an expressionC/C++(29)" error. I am using Visual Studio Code. Any possible reason?
The official name is "the conditional operator", and it is an expression that produces a value - a ? b : c is not shorthand for if (a) b; else c; but a choice between the values b and c.
Rewrite to return the value of the expression:
return n==0 ? 0.0 : (n==1 ? 1.0 : 2*recursively(n-1)+recursively(n-2));
The parentheses around n == 1 ? ... are technically redundant, but make it easier to read.
I sometimes break long lines like this:
return n == 0
? 0.0
: n == 1
? 1.0
: 2 * recursively(n-1) + recursively(n-2);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm following a book called: Game Physics Engine Development. In the book, the author create a custom type(real) to allow him to switch easily between float an double without changing the entire program. He then overload the *= operator to allow a vector dot product.
typedef float real;
#define RealSqrt sqrt
#define RealPow powf;
EDIT: And here's the vector class.
class Vector3D
{
public:
real x;
real y;
real z;
void operator *=(const real value)
{
x *= value;
y *= value;
z *= value;
}
}
My problem is that when I use the macro(RealPow) that I defined with my *= operator, Visual Studio highlight an error telling me that the operator *= is undefined for such parameters. (velocity is a vector)
velocity *= RealPow(damping, duration);
But what I don't understand is than when I write it directly with powf, there's no problem.
velocity *= powf(damping, duration);
EDIT: The error that it gives me is: No operator "*=" matches these operands: Vector3D *= float_cdecl(float _x, float _y)
Am I doing something wrong?
Your define line
#define RealPow powf;
should not have an extra semicolon at the end.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have this error in Dev C. It says there is an error regarding pointers, but I'm not using pointers.
[Error] invalid conversion from 'int*' to 'int' [-fpermissive]
The error is in this line:
E=suma1 + distancias [x,y];
(Where suma1 and E are integers, and distancias is a matrix)
The expression x,y is actually a single value in C and C++. It evaluates both x and y but gives you the single value y. You can see this in action if you try:
#include <stdio.h>
int main (void) {
int a;
a = (1, 2, 3, 4, 5);
printf ("%d\n", a);
return 0;
}
which will output 5.
Hence what your current expression distancias [x,y] is being evaluated is is a simple distancias [y] (because evaluating x here has no side effects), which is why it's complaining about an int pointer being used where an int is expected.
The correct syntax for multi-dimensional arrays would be distancias [x][y].
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a little problem.
Who knows how we can calculate the log
base n with Shift_L or Shift_R?
for example: for n=2 we had this solution:
int log(int n){
int res = 0;
while((n>>=1))
res++;
return res;
}
You don't seem to want the logarithm for a base b, but the largest integer n so that n <= log_b(x). If that's the case, the following function should serve your needs:
int intlog(double base, double x) {
return (int)(log(x) / log(base));
}
well this is rather a math problem instead of an actuall programming problem, if i understand your problem correctly:
log_2 (x) = log_a (x) / log_a (2) where a can be any base.
Therefore you could use the math.h's function log(double)
double res = log(x)/log(2);
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have written a Sign function and am wondering whether it is correct or not (Stupid question to ask, I know!) I'm just interested in knowing whether this is the best method to solve this particular task:
template<typename T>
T sign(T n)
{
if(n < 0) return -1;
if(n > 0) return 1;
return 0;
}
Would this give accurate enough results for large datasets? Can anyone see a problem, that I haven't come across that may arise when putting this into a real-life context?
Thanks
I would change return 0; to return n;. If n is NaN, sign should return NaN, not 0.