对C ++来说很新,我一直在关注3DBuzz.com的中级教程,并试图尝试他们的任务。
目前的教程是关于课程的:http://www.3dbuzz.com/vbforum/sv_showvideo.php?v=37
我正在尝试重载&运算符<<在我想要的时候输出我的'Point'作为流。视频的相关部分从 39:00 开始。
据我所知,我的代码在语法上是相同的(虽然我是新的,所以我可能错过了一些东西)但是我得到了错误:
1> c:\ users \ jack \ documents \ visual studio 2010 \ projects \ myfirstgame \ myfirstgame \ main.cpp(88):错误C2146:语法错误:缺少';'在标识符“myPoint
之前我意识到我在运算符重载函数中声明了实例Point& myPoint ..但我不知道我能在哪里做到这一点所以编译器知道它是什么......如果这是有道理的。
任何帮助表示赞赏!感谢
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(float f_x = 0.0, float f_y = 0.0, float f_z = 0.0);
~Point();
void SetXYZ(float X, float Y, float Z);
void SetX(float X);
void SetY(float Y);
void SetZ(float Z);
void GetXYZ(float &X, float &Y, float &Z);
float GetX();
float GetY();
float GetZ();
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
cout << "Constructor with ARGUMENTS!" << endl;
x = f_x;
y = f_y;
z = f_z;
}
void Point::GetXYZ(float &X, float &Y, float &Z)
{
X = GetX();
Y = GetY();
Z = GetZ();
}
float Point::GetX()
{
return x;
}
float Point::GetY()
{
return y;
}
float Point::GetZ()
{
return z;
}
void Point::SetXYZ(float X,float Y, float Z)
{
SetX(X);
SetY(Y);
SetZ(Z);
}
void Point::SetX(float X)
{
x = X;
}
void Point::SetY(float Y)
{
y = Y;
}
void Point::SetZ(float Z)
{
z = Z;
}
Point::~Point()
{
cout << "We're in the destructor" << endl;
}
ostream &operator <<(ostream &stream, Point &myPoint)
{
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
return stream;
}
void main()
{
float x, y, z; //Declaring floats for use in GetXYZ()
Point myLocation (1,2,-1); //Creating instance and using Point(...) function
cout << myLocation.GetX() << myLocation.GetY() << myLocation.GetZ() <<endl; // Getting xyz values and printing
myLocation.SetXYZ(2,3,-4); //Testing SetXYZ function
cout << myLocation.GetX() << myLocation.GetY() << myLocation.GetZ() <<endl; // Getting xyz values and printing
myLocation.GetXYZ(x, y, z);
cout << x << " " << y << " " << z << endl;
cout << myLocation;
system("PAUSE");
}
编辑:令人难以置信的回应!喜欢这个网站。谢谢大家发现这个^^
答案 0 :(得分:4)
您遗失了<<
:
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
^^
答案 1 :(得分:0)
在第88行,您应该添加&lt;&lt;在myPoint.GetZ();
之前
您的代码:
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
校正:
stream << myPoint.GetX() << " " << myPoint.GetY() << " " << myPoint.GetZ();
答案 2 :(得分:0)
看到这两者之间的差异?
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
stream << myPoint.GetX() << " " << myPoint.GetY() << " " << myPoint.GetZ();
答案 3 :(得分:0)
以下是问题:stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
您在最后<<
和最后" "
之间缺少myPoint
。
答案 4 :(得分:0)
你走了:
ostream &operator <<(ostream &stream, Point &myPoint) { stream << myPoint.GetX() << " " << myPoint.GetY() << " "<< myPoint.GetZ(); return stream; }
请注意额外的“&lt;&lt;”在myPoint.GetZ();
之前