尝试转发声明时"->"是什么意思?
What does '->' mean when trying to forward-declare?
我刚刚开始尝试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((;
其他方式,你会得到一个错误。
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 文件系统:复制功能的速度秘诀是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- 使用 std::vector<boost::shared_ptr<Base_Class>> 或 boost::p tr_vector 的性能注意事项是什么<Base>