我剛開始嘗試C++。當試圖從另一個文件中轉發聲明這個類時,我一直遇到這個錯誤: 對'girl'中成員'get_posx'的請求,它是指針類型'Vex *'(也許你打算使用' - >'? )' - >'在嘗試轉發聲明時意味着什麼?
這些都是我已經包含在我的Qt造物主平原C++項目的兩個文件:
main.cpp中:
#include <iostream>
using namespace std;
class Vex; //Declares Vex class
class Obj //Object class
{
int x, y;
public:
void set_pos (int,int);
void move (int, int);
int get_posx() {return x;}
int get_posy() {return y;}
};
void Obj::set_pos (int pos_x, int pos_y) //Sets X and Y of Obj
{
x = pos_x;
y = pos_y;
}
void Obj::move (int pos_x, int pos_y) //Changes X and Y of Obj
{
x += pos_x;
y += pos_y;
}
int main()
{
Obj guy; //Guy as Obj class
Vex* girl; //Girl as (imported)Vex class
guy.set_pos (0,0);
while(true == true)
{
int tempx, tempy;
cout << girl.get_posx(); //Prints x pos. of girl
cout << "\nX Position: " << guy.get_posx() <<endl; //Prints x pos. of guy
cout << "Y Position: " << guy.get_posy() <<endl; //Prints y pos. of guy
cout << "\nX Change:" << endl;
cin >> tempx; //Asks for x change in guy pos.
cout << "Y Change:" << endl;
cin >> tempy; //Asks for y change in guy pos.
guy.move (tempx, tempy);
}
return 0;
}
s.cpp:
class Vex
{
int x, y;
public:
void exp();
int get_pos() {return x;}
};
void Vex::exp()
{
x = 0;
y = 0;
}
什我是否在這裏做錯了,什麼是' - >',我該如何使用它?
由於' - >'在任何前向聲明中都沒有使用,因此標題具有誤導性。 – greatwolf 2014-12-06 05:58:17