Expression must have integral or unscoped enum type.

Mar 2, 2014 at 9:05pm
Hello everyone I am writing a small program for class that measures 3d distance.
I have been trying for figure out what I'm doing wrong for the last couple hours.
Here is my code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

float dist3(float ux, float uy, float uz, float vx, float vy, float vz);

int main()
{
	cout << "The distance between (1, 2, 3) and (0, 0, 0) = " << dist3(0, 0, 0, 1, 2, 3) << endl;

	system("Pause");
}

float dist3(float ux, float uy, float uz, float vx, float vy, float vz)
{
	float answer = 0.0f;
	float distance = 0.0f;
	distance = (vx - ux) ^ 2 + (vy - uy) ^ 2 + (vz - uz) ^ 2;
	answer = sqrt(distance);

	return answer;
}


The error is at line 20.

Thanks
Mar 2, 2014 at 9:07pm
(vx - ux) ^ 2
Why are you using bitewise xor on float?
Mar 2, 2014 at 9:21pm

Why are you using bitewise xor on float?


No idea what that is.
Mar 2, 2014 at 9:27pm
No idea what that is.
Then why are you using it?
That ^ it is bitwise xor operator
http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/
Mar 2, 2014 at 9:34pm
you cant use ^ to do exponential need to use pow() function
Mar 2, 2014 at 9:41pm
to do exponential need to use pow() function
For squares it is way faster to just multiply value by itself.
Mar 2, 2014 at 9:43pm
Oh, alright Thanks, I was thinking ^ was like it was in regular math.
Last edited on Mar 2, 2014 at 9:46pm
Mar 2, 2014 at 9:52pm
Without being able to use ^ like that then I have no idea.
Let me quote wiki:
a square is the result of multiplying a number by itself. [..] Squaring is the same as raising to the power 2
( http://en.wikipedia.org/wiki/Square_%28algebra%29 )
^ doesn't mean what it does in math
It doesn't mean that in math too. It adopted to have that meaning in some programming languages and in some systems of writing math formulas in simple text editors or in typewriters
http://en.wikipedia.org/wiki/Exponentiation#In_programming_languages

Just use (vx - ux)*(vx - ux) etc. here.
Mar 2, 2014 at 9:54pm
I'm currently not on my PC with VS but this is what I've come up with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

float dist3(float ux, float uy, float uz, float vx, float vy, float vz);

int main()
{
	cout << "The distance between (1, 2, 3) and (0, 0, 0) = " << dist3(0, 0, 0, 1, 2, 3) << endl;

	system("Pause");
}

float dist3(float ux, float uy, float uz, float vx, float vy, float vz)
{
	float answer = 0.0f;
	float distance = 0.0f;
	distance = (vx - ux) * (vx - ux) + (vy - uy) * (vy - uy) + (vz - uz) * (vz - uz);
	answer = sqrt(distance);

	return answer;
}


So I assume the program is now correct?
Last edited on Mar 2, 2014 at 9:55pm
Mar 2, 2014 at 9:59pm
Yes, it is correct. Some points:

1) It is good idea to initialize variable with correct value from the beginning:
float distance2 = (vx - ux)*//...
2) You can get rid of another variable by returning answer immediately:
return std::sqrt(distance2);
3) You can actually make it in 1 line, but it will impair readability too much, I think
Mar 2, 2014 at 9:59pm
Alright Thanks, you keep answering my questions while i'm typing which is why I have asked things you just posted.

And by ^ in math I didn't mean like when you write it out on paper, I meant like if you type it in google or something.
Mar 2, 2014 at 10:04pm
I meant like if you type it in google
Well, that is one of the systems used. It seems that the caret symbol had been started to use like that after advent of typewriters. Before that up arrow ↑ was often used in manuscrypts. I actually like it more. Sadly up arrow can easily be mistaken for Sheffer stroke now.
Mar 2, 2014 at 10:21pm
Actually after thinking about it I'm not sure why I didn't just multiply it by itself from the start, that's how I did an exercise program yesterday. Must be because I had just looked up the formula for 3d distance and it showed the ^.
Topic archived. No new replies allowed.