C++ operator test failed

I have been thinking for a while now why doesn't my operators work properly when I get to the condition if (v4 * v5 == x1 * x2 + y1 * y2) in my main. But just couldn't figure it out. So frustrating. Can someone enlighten me on it? Thanks!

Output: - * operator test failed

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class fvector2D: public std::complex<float>
{
	public:
	fvector2D(float x=0, float y=0) : std::complex<float>(x, y)
	{
	 
	}
	
	fvector2D(const fvector2D& Vector2d): std::complex<float>(Vector2d)
	{
 	}
  
	float X()const
	{ 
		return mreal();
	}
	float Y()const
	{
		return mimag();
	}
 
	float operator* (fvector2D& factor){
        float result;
        result = X() * factor.X();
        result += Y() * factor.Y();
        return result;
       }
 
};

 

int main(){
        float x1 = frand();
	float y1 = frand();
	float x2 = frand();
	float y2 = frand();
	const fvector2D  v3(x1, y1);
	const fvector2D v5(x2, y2);
	if (v3 * v5 == x1 * x2 + y1 * y2) //condition failed
	{
		++count;
	}
	else
	{
		std::cout << "  - * operator test failed" << std::endl;
	}

}
Last edited on
@playpro,
One has to to do rather a lot to get your code to compile: it's missing headers. count isn't either declared or initialised. You have apparently defined a class fvector2D, yet tried to initialise vector2d. Please always upload the code that is causing the problem, not some random variant of it.

v4 * v5 is a complex number.
x1 * x2 + y1 * y2 is a float.
They won't compare equal.

See what the following interpretation of your code gives:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <complex>

class fvector2D: public std::complex<float>
{
	public:
	fvector2D(float x=0, float y=0) : std::complex<float>(x, y)
	{
	 
	}
	
	fvector2D(const fvector2D& Vector2d): std::complex<float>(Vector2d)
	{
 	}
	fvector2D(const std::complex<float> &other): std::complex<float>(other){}
 
	float X()const
	{ 
		return real();
	}
	float Y()const
	{
		return imag();
	}
 
	float operator* (fvector2D& factor){
	    std::cout << "I'm here\n";
        float result;
        result = X() * factor.X();
        result += Y() * factor.Y();
        return result;
       }
 
};

fvector2D operator+(const fvector2D& lhs, const fvector2D& rhs)
{
	return static_cast<const std::complex<float> &>( lhs) + static_cast<const std::complex<float> &>(rhs);
}

int main(){
    float x1 = 4, y1 = 5, x2 = 6, y2 = 7;
    int count = 0;
	const fvector2D v4(x1, y1);
	const fvector2D v5(x2, y2);
	if (v4 * v5 == x1 * x2 + y1 * y2) //condition failed
	{
		++count;
	}
	else
	{
		std::cout << "  - * operator test failed" << std::endl;
	}
	std::cout << v4 * v5 << "   " << x1 * x2 + y1 * y2;
}


  - * operator test failed
(-11,58)   59 
Last edited on
Topic archived. No new replies allowed.