我刚开始尝试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