后來測試了一下不是無法調用函數的問題下面上代碼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括起來
- 1 回答
- 0 關注
- 237 瀏覽
添加回答
舉報
0/150
提交
取消