C++Visual Studio 将类方法解释为静态
C++ Visual Studio interprets the class method as static
Visual Studio将类方法解释为静态,但它们不是。当我构建下面的代码时,我有 110 个错误。
错误:有错误的照片1.,有错误的照片2。
可能错误地声明了这些方法?
//header
class Player {
public:
Player(int x, int y) {
PlayerX = x;
PlayerY = y;
};
void doAction(int input, Mapa *mapa);
int getDirection();
Vector2 getPosition();
int
PlayerX, PlayerY, direction;
void turn(int dir);
void move(int move, Vector2 mapSize, Mapa *mapa);
Vector2 getCordInFrontOfCharacter();
Vector2 getCordBehindCharacter();
};
和 cpp 文件:
#include "Vector2.h"
#include "Player.h"
#include "Mapa.h"
using namespace std;
int PlayerX = 0, PlayerY = 0;
int direction = 0;
void Player::doAction(int input, Mapa *mapa) {
if (input == (char)72)
this->move(1, *mapa->mapSize, mapa);
else if (input == (char)80)
this->move(-1, *mapa->mapSize, mapa);
if (input == (char)75)
this->turn(-1);
else if (input == (char)77)
this->turn(1);
}
void Player::turn(int dir) {
if (dir < 0)
dir = 2 - dir;
direction = (direction + dir) % 4;
}
void Player::move(int move, Vector2 mapSize, Mapa *mapa) {
if (
move = 1
&& getCordInFrontOfCharacter().y - 1 >= 0
&& getCordInFrontOfCharacter().x - 1 >= 0
&& getCordInFrontOfCharacter().y - 1 < mapSize.y
&& getCordInFrontOfCharacter().x - 1 < mapSize.x
&& mapa->_Mapa[getCordInFrontOfCharacter().y - 1][getCordInFrontOfCharacter().x - 1] == '0') {
if (this->direction == 0)
this->PlayerY -= move;
else if (this->direction == 2)
this->PlayerY += move;
else if (this->direction == 1)
this->PlayerX += move;
else if (this->direction == 3)
this->PlayerX -= move;
if (this->PlayerY < 1)
this->PlayerY = 1;
if (this->PlayerX < 1)
this->PlayerX = 1;
if (this->PlayerY > mapSize.y)
this->PlayerY = mapSize.y;
if (this->PlayerX > mapSize.x)
this->PlayerX = mapSize.x;
}
else if (
move = 1
&& this->getCordBehindCharacter().y - 1 >= 0
&& this->getCordBehindCharacter().x - 1 >= 0
&& this->getCordBehindCharacter().y - 1 < mapSize.y
&& this->getCordBehindCharacter().x - 1 < mapSize.x
&& mapa->_Mapa[this->getCordBehindCharacter().y - 1][this->getCordBehindCharacter().x - 1] == '0') {
if (this->direction == 0)
this->PlayerY -= move;
else if (this->direction == 2)
this->PlayerY += move;
else if (this->direction == 1)
this->PlayerX += move;
else if (this->direction == 3)
this->PlayerX -= move;
if (this->PlayerY < 1)
this->PlayerY = 1;
if (this->PlayerX < 1)
this->PlayerX = 1;
if (this->PlayerY > mapSize.y)
this->PlayerY = mapSize.y;
if (this->PlayerX > mapSize.x)
this->PlayerX = mapSize.x;
}
}
int Player::getDirection() {
return this->direction;
}
Vector2 Player::getPosition() {
return Vector2(this->PlayerX, this->PlayerY);
}
Vector2 Player::getCordInFrontOfCharacter() {
if (this->direction == 2)
return Vector2(this->PlayerX, this->PlayerY + 1);
else if (this->direction == 0)
return Vector2(this->PlayerX, this->PlayerY - 1);
else if (this->direction == 3)
return Vector2(this->PlayerX - 1, this->PlayerY);
else if (this->direction == 1)
return Vector2(this->PlayerX + 1, this->PlayerY);
return Vector2(0, 0);
}
Vector2 Player::getCordBehindCharacter() {
if (direction == 2)
return Vector2(this->PlayerX, this->PlayerY - 1);
else if (this->direction == 0)
return Vector2(this->PlayerX, this->PlayerY + 1);
else if (this->direction == 3)
return Vector2(this->PlayerX + 1, this->PlayerY);
else if (this->direction == 1)
return Vector2(this->PlayerX - 1, this->PlayerY);
return Vector2(0, 0);
}
感谢您的帮助。
Visual Studio compilater 显然对这个函数签名感到困惑
void Player::move(int move, Vector2 mapSize, Mapa *mapa)
其中,参数和函数本身使用相同的名称。更改它以使它们与众不同:
void Player::move(int move_, Vector2 mapSize, Mapa *mapa)
// ^
在 cpp 文件中,我用"::"初始化非静态方法
在 c++ 中初始化方法几乎是不可能的。我是说班上的人,嗯,那就错了。
如果您有非静态成员,则无法执行此操作:
int foo::member = 0; // error, member is non-static
每个foo对象都有自己的int member
,所以它们之间不共享。您必须为构造函数中的每个对象设置它(也许默认值就是您要查找的(:
foo::foo (int _mem = 0, ... ) : member(_mem) {...}
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 请解释"函数1(p1,p2,p3);"的输出
- #为""定义宏;静态";针对不同的上下文
- cmake如何在fedora工作站中找到boost静态库包
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- cmake在我的项目中所需的所有静态库都不成功
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- C++从另一个类访问公共静态向量的正确方法是什么
- 基于boost的程序的静态链接——zlib问题
- 如何在 C++11 中解释静态代码
- 指向 POD 类型的指针之间的静态转换与重新解释转换
- 有人可以解释为什么以下代码中的总数 = 7 吗?使用静态整数和
- C++ 需要解释“静态常量 WCHAR*”
- C++Visual Studio 将类方法解释为静态
- 该"Named Constructor Idiom"似乎与静态方法无法访问非静态成员函数的规则相矛盾。有什么解释吗?
- 在void*和void*之间进行强制转换时,混合使用静态强制转换和重新解释强制转换是否不安全?
- 静态/动态/常量/重新解释_可以在未评估的上下文中使用吗
- 关于基类中的显式模板实例化和静态变量:编译器错误或规范的有效解释
- 重新解释类型转换和静态类型转换的概念