如何从外部函数 C++ 访问动态结构?
Posted
技术标签:
【中文标题】如何从外部函数 C++ 访问动态结构?【英文标题】:How to Access a Dynamic struct from an external function C++? 【发布时间】:2013-04-25 05:29:54 【问题描述】:我正在尝试制作一个非常简单的基于文本的游戏,当我尝试从外部函数访问动态结构时遇到错误。我在头文件中初始化了所有变量和结构,并在主函数中声明了一个动态分配。但我仍然有错误。我错过了什么吗?这是我的代码。
==================主函数“new.cpp”====================
#include <stdlib.h>
#include <iostream>
#include <string>
#include "game.h"
using namespace std;
difficulty _df_1;
player* player_1 = new player[3];
int main()
//initialize player stats
player_1->hp = 100;
player_1->life = 3;
player_1->mana = 50;
player_1->player_id = 1;
player_1->level = 1;
player_1->player_name= "emmet";
//..end
int turn=1;
cout << "What is your name? <<<";
getline(cin,player_1->player_name,'\n');
cout << "Choose a difficulty level: [0]Easy [1]Normal [2]Godlike" << endl;
int ch;
cin >> ch;
switch(ch)
case 0:
cout << "Scardy Cat chose EASY." << endl;
break;
case 1:
cout << "A really nice way to start. NORMAL" << endl;
break;
case 2:
cout << "Overly Manly Man is playing GODLIKE." << endl;
break;
default: cout << "I wonder how you can play this game if you can even read simple instructions."<< endl;return 0; break;
while(turn == 1)
char ch;
cout << "What do you want to do now? \n <<<<";
cin >> ch;
cin.ignore(5,'\n');
switch(ch)
case 'a': case 'A':
player_stat();
break;
case 'v': case 'V':
cheat_menu();
break;
case 'x': case 'X':
return 0;
break;
case '`':
break;
default: cout << "We were unable to process your request. Please try again" << endl; break;
delete player_1;
return 0;
void cheat_menu()
cout << "CHEATERS WILL ROT IN THE DEEPEST DEPTHS OF TARTARUS." << endl;
cout << "Enter Code:" << endl;
string cheat;
getline(cin,cheat,'\n');
if(cheat == "poo")
system("sleep 3");
cout << "Cheat Activated.." << endl;
player_1->hp += 1000;
player_1->level += 10;
player_1->mana += 1000;
player_stat();
else
cout << "Wrong cheat code.." << endl;
//system("sleep 3");
//system("clear");
==================结束主函数==============
=======外部函数“player_stat.cpp”===========
#include <iostream>
#include "game.h"
using namespace std;
void player_stat()
cout << "Name: " << player_1->player_name << endl
<< "Hp: " << player_1->hp << endl
<< "Life: " << player_1->life << "\t Mana: " << player_1->mana << endl
<< "Level: " << player_1->level << "\t XP: " << player_1->xp
<< endl;
//system("sleep 3");
//system("clear");
==================结束外部函数==============
==========头文件“game.h”===================
#ifndef _GAME_
#define _GAME_
#include "player_stat.cpp"
using namespace std;
//function prototypes...
void player_stat();
void cheat_menu();
//structs for player and NPC
struct player
string player_name;
int life;
double atk;
double hp;
double mana;
int player_id;
int level;
long int xp;
string weapon_name;
double weapon_damage;
;
enum difficulty EASY,NORMAL,GODLIKE;
#endif
===========结束头文件=======================
这些是我得到的错误。请不要介意 int main() 中的其他遗漏函数。 :P
In file included from game.h:4
from new.cpp
in function `void player_stat()':
`player_1' undeclared (first use in this function)
(Each undeclared identifier is reported only once for each function it appears in.)
At global scope:
`player*player_1' used prior to declaration
【问题讨论】:
您使用new player[3]
创建了三个player
对象,然后使用指针仅访问第一个。如果只需要一个播放器,则将其声明为普通变量,无需在堆上分配。
@n.m.我不得不取消链接到png-screencap它。
如果我不这样做,我将无法动态更改结构成员。新玩家[3]。我这样做只是为了表明我只希望游戏中最多有 3 名玩家,因为我正计划为其添加一些套接字功能。多人游戏之类的。
如果你想要多个播放器,那么我推荐std::vector
。或者std::array
,如果你想要固定数量的玩家。
当然..我会试试..谢谢:)
【参考方案1】:
您使用extern
存储说明符声明变量:
extern player* player_1;
这告诉编译器player_1
变量是在其他地方定义的,链接器稍后会解析它。
正如我注意到的,您实际上分配了 三个 播放器对象。如果你只需要一个,那么不要分配三个:
player* player_1 = new player;
但是,这也不是必需的,声明一个普通的非指针变量也可以正常工作:
player player_1;
然后在其他文件中使用extern
告诉编译器该变量是在其他地方定义的:
extern player player_1;
【讨论】:
我知道不需要将其声明为动态的。但是..在我编写此代码的早期阶段.. struct 成员的值似乎没有改变。您可能会在 main() 中遇到 cheat_menu() 函数。这就是该函数的目的。测试结构是否会根据玩家活动而改变。所以我需要将它声明为动态的,以便在游戏中值会发生变化。尝试删除该分配部分。你会注意到作弊不会奏效..无论如何谢谢..我会尝试考虑你的建议:) @EmmetCooper 我仍然看不到任何动态分配它的理由。做例如player_1.hp += 1000;
与 player_1->hp += 1000;
没有区别。问题可能是您在参数中将对象作为 copies 而不是例如参考。
哦..我现在明白了..我试图制作一个简单的程序来测试你所说的..你是对的..谢谢:)..我会删除分配..也许它会让我的工作更轻松..毕竟我对 c++ 还很陌生:p【参考方案2】:
添加
外部玩家* player_1;
到game.h
这将使 player_1 变量在所有模块中可用。
也删除
#include "player_stat.cpp"
你不应该在头文件中包含任何 cpp 文件
【讨论】:
当然.. 我会试试的.. :).. 感谢您的超快速回复。 :)以上是关于如何从外部函数 C++ 访问动态结构?的主要内容,如果未能解决你的问题,请参考以下文章