如何从外部函数C ++访问动态结构?

时间:2013-04-25 05:29:55

标签: c++ dynamic data-structures dev-c++

我正在尝试制作一个非常简单的基于文本的游戏,当我尝试从外部函数访问动态结构时遇到错误。我初始化了我的头文件中的所有变量和结构,并在main函数中声明了一个动态alloc。但我仍然有错误。我错过了什么吗?这是我的代码。

==================主要功能“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

2 个答案:

答案 0 :(得分:0)

添加

  

extern player * player_1;

到game.h

这将使所有模块中的player_1变量可用。

也删除

  

#include“player_stat.cpp”

你不应该在头文件中包含任何cpp文件

答案 1 :(得分:0)

使用extern存储说明符声明变量:

extern player* player_1;

这告诉编译器player_1变量是在其他地方定义的,链接器稍后会解析它。


另外正如我所说,你实际上分配了三个播放器对象。如果你只需要一个,那么不要分配三个:

player* player_1 = new player;

但是,这也不需要,声明一个正常的非指针变量也可以正常工作:

player player_1;

然后在其他文件中使用extern告诉编译器该变量是在其他地方定义的:

extern player player_1;