什么' - >'试图向前宣布时意味着什么?

时间:2014-12-06 05:51:46

标签: c++ forward-declaration

我刚刚开始试用C ++。尝试从另一个文件转发声明此类时,我一直遇到此错误: 请求会员' get_posx'在' girl'中,它是指针类型' Vex *' (也许你打算使用' - >'?)

这些是我在Qt-Creator Plain 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;
}

我在这里做错了什么,以及&#39; - &gt;&#39;,我该如何使用它?

2 个答案:

答案 0 :(得分:3)

我认为你最好先学习C ++,因为你似乎缺乏基础知识;没有冒犯意味着,只是试图帮助它。您在脑海中存在一些误解以及实施问题。我甚至不知道从哪里开始。让我们看看:

  • 堆和堆栈分配对象之间的区别(也就是指针与非指针)。

    Vex* girl; //Girl as (imported)Vex class
    ...
    cout << girl.get_posx(); //Prints x pos. of girl
    

    在C ++中通过->访问指针对象成员,因此您应该将代码修改为:

    cout << girl->get_posx(); //Prints x pos. of girl
    //          ^^
    
  • 何时可以使用前向声明。

    class Vex; //Declares Vex class
    

    当您想要访问类成员时,这还不够。您需要通过include指令包含类定义,如下所示:

    #include "vex.h"
    
  • 在源文件(aka。cpp)中声明类似的类

    这通常是错误的,尽管不是最终肯定的。如果你这样做,你将无法在不包含源文件的情况下轻松地重复使用它,这不是真正推荐的。

  • 严格来说,没有&#34; import&#34;在C ++中。

    Vex* girl; //Girl as (imported)Vex class
    //                    ^^^^^^^^
    

    可以在其他编程语言(例如python)中找到导入术语,但这些术语与包含的内容不同。

  • Overcommenting

    这个评论不仅有点不全面,甚至毫无意义。争取自我记录代码。

  • 您认为它与Qt有任何关联。

    它不是,即使你这样标记它。它是通用的(和基本的)C ++。

  • 不使用const作为不更改成员的方法。

    这对于在代码中获取成员这样做是很好的做法,即:

    int get_posx() const {return x;}
    //             ^^^^^
    int get_posy() const {return y;}
    //             ^^^^^
    

    ......同样地:

    int get_pos() const {return x;}
    //            ^^^^^
    
  • 如何编写阻塞永久循环。

     while(true == true)
    

    虽然有效,但这是荒谬的。

    `while(1)`
    

    或只是

    `forever()`
    
    Qt中的

    会更优雅。

  • 编码风格不一致(例如空间使用)

    void set_pos (int,int);
    //          ^     ^
    void move (int, int);
    //       ^     ^
    int get_posx() {return x;}
    //          ^
    
  • 不使用缩进

    您的源代码似乎是左对齐的,但这不是word文档。虽然,C和C ++使用括号,因此它将起作用,例如,在Python中,很难理解。

  • 您似乎并不了解构造函数是什么。

    Obj guy; //Guy as Obj class
    ...
    guy.set_pos (0,0);
    

    在C ++中,你建立一个构造函数或者至少是&#34; init&#34;这种初始化的方法。严格来说,在C ++ 11上,您甚至可以避免这种情况,但重点是您不要通过设置方法初始化成员。

答案 1 :(得分:0)

girl被声明为指向对象Vex的指针:

Vex * girl

因此你需要使用 - &gt;运算符而不是。解决女孩对象成员:

姑娘 - &GT; get_posx();

另外,因为它是指针,你需要用适当的对象初始化它:

Vex * girl = new Vex();

您将收到错误的其他方式。

相关问题