Overloaded operator undefined [closed] - c++

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.

Related

get the memory address instead of double value [closed]

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
I just practice on c++ and want to get the area of circle from a given radius but the result giving me a memory address instead of value
#include <iostream>
#include<math.h>
using namespace std;
int main() {
double a, n, r;
n = 3.14159;
cin >> r;
a = pow(r,r) * n;
cout << "A=" << a<<endl;
}
while the input is 100.64 i got the output
A=1.13759e+202
but the result giving me a memory address instead of value
while the input is 100.64 i got the output
A=1.13759e+202
Your assumption is wrong. That is not a "memory address". That is the correct result of π × rʳ.
However, your calculation is not the correct one for area of a circle. Correct formula is π × r².
Bonus hint: r * r is typically better than calling std::pow.
Bonus hint 2: C++20 has constant std::numbers::pi in the <numbers> header. It provides you with the closes representable approximation of π.

Modern way to use SIMD instructions in C++ using g++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm building a ray tracer and in many cases I need to make additions and multiplications on three floats.
In such cases I've been doing it the naive way:
class Color{
float mR, mG, mB;
...
Color operator+(const Color &color) const
{
return Color(mR + color.mR,
mG + color.mG,
mB + color.mB);
}
Color operator*(const Color &color) const
{
return Color(mR * color.mR / COLOR_MAX,
mG * color.mG / COLOR_MAX,
mB * color.mB / COLOR_MAX);
}
}
This would also happen in equivalent classes such as Point or Vect3.
Then I heard about SIMD instructions and they look like a good fit for what I'm doing. So, of course, I googled it and found this piece of code:
typedef int v4sf __attribute__((mode(V4SF))); // Vector of three single floats
union f4vector
{
v4sf v;
float f[4];
};
Which first of all uses an extra four I don't need right now. But then gcc warns me that:
specifying vector types with __attribute__ ((mode)) is deprecated
I'd like to know how to do this in C++14 (if it even makes a difference at all) and I can't seem to find any other way of doing it.

If Statement Alternative (Conditional Operator) [closed]

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;

error in C for simulated templade [closed]

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].

Calculate the log base n with Shift left or Shift right [closed]

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);