后來測試了一下不是無法調(diào)用函數(shù)的問題下面上代碼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()函數(shù)的代碼放在主循環(huán)里 就可以調(diào)用與執(zhí)行但是 如果在主循環(huán)里調(diào)用Player::Move()函數(shù)的話 Move()函數(shù)可以被調(diào)用與執(zhí)行 但是不能給Player::Player_x Player::Player_y Player::Player_z 賦值這三個數(shù)據(jù)都是float的
1 回答

守候你守候我
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個贊
用法有問題,namespace里面的定義, 變量定義放在cpp中,看你定義static,既然不打算變量給外部用,那頭文件中就連聲明都不需要了。
里面的函數(shù)也要用namespace括起來
- 1 回答
- 0 關(guān)注
- 249 瀏覽
添加回答
舉報
0/150
提交
取消