尝试转发声明时"->"是什么意思?

What does '->' mean when trying to forward-declare?

本文关键字:gt 是什么 意思 转发 声明      更新时间:2023-10-16

我刚刚开始尝试C++。尝试从另一个文件转发声明此类时,我不断遇到此错误:请求"女孩"中的成员"get_posx",其指针类型为"Vex*"(也许您打算使用"->"?

这是我包含在Qt-Creator Plain C++项目中的两个文件:

主.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;
}

我在这里做错了什么,什么是"->",我该如何使用它?

我认为你最好先学习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"
    
  • 在源文件中声明类似的类(又名.cpp(

    这通常是错误的,尽管最终不确定。如果这样做,如果不包含源文件,您将无法轻松重用它,这实际上并不推荐。

  • 严格来说,C++没有"进口"这样的东西。

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

    导入可以在其他编程语言(如 python(中找到,但这些术语的工作方式与包含不同。

  • 过度评论

    这种评论不仅有些不全面,甚至毫无意义。努力实现自我记录代码。

  • 你认为它以任何方式与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++中,您为此类初始化建立一个构造函数或至少"init"方法。严格来说,打开 C++11 时,您甚至可以为此避免这种情况,但关键是您不通过设置方法初始化成员。

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

女郎

因此,您需要使用 -> 运算符而不是 . 来寻址女孩对象成员:

女孩->get_posx((;

另外,因为它是指针,所以你需要用正确的对象初始化它:

Vex* 女孩 = 新 Vex((;

其他方式,你会得到一个错误。