對C++來說很新穎,我一直在關注3DBuzz.com上的中級教程,並試圖嘗試他們的任務。C++:簡單的錯誤,我不能發現! C2146
當前教程是關於類:http://www.3dbuzz.com/vbforum/sv_showvideo.php?v=37
我想< <超載&運營商輸出我的「點」的流時,我想。視頻的相關部分從39:00開始。
至於我可以告訴我的代碼在語法上是相同的(雖然我是新的,所以我可能失去了一些東西),但我得到的錯誤:
1> C:\用戶\千斤頂\文件\ visual studio 2010 \ projects \ myfirstgame \ myfirstgame \ main.cpp(88):錯誤C2146:語法錯誤:缺少';'符前「myPoint
我意識到,我宣佈在運算符重載函數實例點& 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");
}
編輯:令人難以置信的響應!已經愛這個網站。謝謝大家誰看準了這一點^^
我相信你需要聲明它是一個「朋友」才能使它工作。另外,Point&參數應該被聲明爲const。另外,除非這是嵌入式系統的代碼,否則main()必須返回int,否則代碼將無法在C++編譯器上編譯。 – Lundin 2011-03-07 10:55:10
如果本教程實際上建議使用'namespace std;'這是一個錯誤的教程。 – 2011-03-07 10:58:19
@Lundin:運營商不訪問任何非公開成員,所以它不需要成爲朋友。 – 2011-03-07 10:59:11