为了账号安全,请及时绑定邮箱和手机立即绑定

在主循环里无法调用Player::Move() 可是生成没有错误啊?为什么?

在主循环里无法调用Player::Move() 可是生成没有错误啊?为什么?

C++
慕侠2389804 2023-04-24 22:18:50
后来测试了一下不是无法调用函数的问题下面上代码Player.h----------#pragma once#include "BasicInclude.h"namespace Player {static float Player_x;static float Player_y;static float Player_z;void Move();}----------Player.cpp----------#include "Player.h"using namespace Player;void Player::Move() {if (glfwGetKey(MainWindow, GLFW_KEY_W)) { Player::Player_z += 0.01; }else if (glfwGetKey(MainWindow, GLFW_KEY_S)) { Player::Player_z -= 0.01; }else if (glfwGetKey(MainWindow, GLFW_KEY_A)) { Player::Player_x += 0.01; }else if (glfwGetKey(MainWindow, GLFW_KEY_D)) { Player::Player_x -= 0.01; }else if (glfwGetKey(MainWindow, GLFW_KEY_SPACE)) { Player::Player_y += 0.01; }else if (glfwGetKey(MainWindow, GLFW_KEY_LEFT_SHIFT)) { Player::Player_y -= 0.01; }else if (glfwGetKey(MainWindow, GLFW_KEY_RIGHT_SHIFT)) { Player::Player_y -= 0.01; }}----------MineCubeWorld.cpp----------int main() {glfwInit();MainWindow = glfwCreateWindow(WinWidth, WinHeight, WinTitle.c_str(), NULL, NULL);glfwMakeContextCurrent(MainWindow);do {Player::Move();printf("X: %f Y: %f Z: %f \n", Player::Player_x, Player::Player_y, Player::Player_z);glfwPollEvents();if (glfwGetKey(MainWindow, GLFW_KEY_ESCAPE)) { exit(EXIT_SUCCESS); }} while (!glfwWindowShouldClose(MainWindow));glfwTerminate();return 0;}问题描述:如果把Player::Move()函数的代码放在主循环里 就可以调用与执行但是 如果在主循环里调用Player::Move()函数的话 Move()函数可以被调用与执行 但是不能给Player::Player_x Player::Player_y Player::Player_z 赋值这三个数据都是float的
查看完整描述

1 回答

?
守候你守候我

TA贡献1802条经验 获得超10个赞

用法有问题,namespace里面的定义, 变量定义放在cpp中,看你定义static,既然不打算变量给外部用,那头文件中就连声明都不需要了。
里面的函数也要用namespace括起来

查看完整回答
反对 回复 2023-04-27
  • 1 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信