内联功能帮助 [英] Inline function help
本文介绍了内联功能帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码是包含3个点(x,y,z)的Vector类的代码.该代码中有2个错误,问题的根源在第123行,该行是内联Vector3运算符*(float k,const Vector3& v)
如果有人可以帮助我,那真是太好了.感谢您抽出宝贵的时间阅读本文.愿上帝保佑你们.
Zvjezdan Veselinovic
This code is code for a Vector class that holds 3 points (x, y, z). This code has 2 errors in it and the source of the problem is on line 123 which is inline Vector3 operator *(float k, const Vector3 &v)
If anyone can help me out that would be really wonderful. Thank you for taking your time to read this. God bless you guys.
Zvjezdan Veselinovic
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdlib>
#define inline
using namespace std;
int main()
{
class Vector3
{
public: float x, y, z;
Vector3() {}
Vector3(const Vector3 &a) : x(a.x), y(a.y), z(a.z) {}
Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz) {}
Vector3 &operator = (const Vector3 &a)
{
x = a.x;
y = a.y;
z = a.z;
return *this;
}
bool operator ==(const Vector3 &a) const
{
return x==a.x && y ==a.y && z==a.z;
}
bool operator !=(const Vector3 &a) const
{
return x != a.x || y != a.y || z != a.z;
}
void zero()
{
x = y = z = 0.0f;
}
Vector3 operator -() const
{
return Vector3(-x, -y, -z);
}
Vector3 operator +( const Vector3 &a) const
{
return Vector3(x + a.x, y + a.y, z + a.z);
}
Vector3 operator -( const Vector3 &a) const
{
return Vector3(x - a.x, y - a.y, z - a.z);
}
Vector3 operator *(float a) const
{
return Vector3(x * a, y * a, z * a);
}
Vector3 operator /(float a) const
{
float oneOverA = 1.0f / a;
return Vector3(x * oneOverA, y * oneOverA, z * oneOverA);
}
Vector3 &operator +=(const Vector3 &a)
{
x += a.x;
y += a.y;
z += a.z;
return *this;
}
Vector3 &operator -=(const Vector3 &a)
{
x -= a.x;
y -= a.y;
z -= a.z;
return *this;
}
Vector3 &operator *=(float a)
{
x *= a;
y *= a;
z *= a;
return *this;
}
Vector3 &operator /=(float a)
{
float oneOverA = 1.0f / a;
x *= oneOverA;
y *= oneOverA;
z *= oneOverA;
return *this;
}
void normalize()
{
float magSq = x*x + y*y + z*z;
if(magSq > 0.0f)
{
float oneOverMag = 1.0f / sqrt(magSq);
x *= oneOverMag;
y *= oneOverMag;
z *= oneOverMag;
}
else if(magSq < 0.0f)
{
cout << "There has got to be an error." << endl;
cout << endl;
normalize();
}
}
float operator *(const Vector3 &a) const
{
return x * a.x + y * a.y + z * a.z;
}
inline float vectorMag(const Vector3 &a)
{
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
inline Vector3 crossProduct(const Vector3 &a, const Vector3 &b)
{
return Vector3( a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
inline Vector3 operator *(float k, const Vector3 &v)
{
return Vector3(k * v.x, k * v.y, k * v.z);
}
inline float distance( const Vector3 &a, const Vector3 &b)
{
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return sqrt(dx * dx + dy * dy + dz * dz);
}
};
cout << endl;
system("pause");
return 0;
}
推荐答案
此类运算符必须在类外部定义才能正常工作.
Such an operator must be defined outside of the class to works as intended. It will also typically also need to be friend.
运算符*不能有两个参数:
The operator * cannot have two parameters:
inline Vector3 operator * (float k)
{
return Vector3(k * x, k * y, k * z);
}
用这种方式做.
问候.
Do it in this way.
Regards.
值得一提的是,内联只是对编译器的提示.内联您不告诉的内容是免费的,并且可以决定不内联您要告知的内容.
It''s worth mentioning that inline is only a hint to the compiler. It is free to inline stuff you don''t tell it to, and decide not to inline where you DO tell it to.
这篇关于内联功能帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文