非静态成员引用C ++

时间:2018-05-17 01:30:57

标签: c++ visual-c++

我遇到了一些问题,我正在尝试让这个问题起作用,但是我在使用String获取名称时遇到了一些问题。

我收到以下错误: 非静态成员引用必须与特定对象相关 在

std::string GetName(uint32 entry) //this is the error causer.
    {
        return sObjectMgr->GetItemTemplate(entry)->Name1;
    };

然而,Getname函数在每个其他类

上给我一个错误
#include "ScriptMgr.h"
#include "Creature.h"
#include "ScriptedCreature.h"
#include "ScriptedGossip.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "WorldSession.h"

struct tokenData { uint32 TAKE_ENTRY, TAKE_AMOUNT, GIVE_ENTRY, GIVE_AMOUNT; };
struct tokenData Tokens[] =
{
    {191201, 10, 191202, 1},
    {191202, 1, 191201, 10},

    {191202, 25, 191203, 1},
    {191203, 1, 191202, 25},

    {191203, 50, 191204, 1},
    {191204, 1, 191203, 50},

    {191204, 100, 191205, 1},
    {191205, 1, 191204, 100}
};
const uint32 tokensSize = sizeof Tokens / sizeof(*Tokens);


class Exchanger_npc : public CreatureScript
{
public:
    Exchanger_npc() : CreatureScript("Exchanger_npc") { }


    std::string GetName(uint32 entry) //this is the error causer.
    {
        return sObjectMgr->GetItemTemplate(entry)->Name1;
    };

    static bool OnGossipHello(Player* player, Creature* creature)
    {
        for (uint32 i = 0; i < tokensSize; ++i)
        {
            std::ostringstream ss;
            ss << "Swap " << Tokens[i].TAKE_AMOUNT << " x " << GetName(Tokens[i].TAKE_ENTRY) << " to " << Tokens[i].GIVE_AMOUNT << " x " << GetName(Tokens[i].GIVE_ENTRY);
            AddGossipItemFor(player, 0, ss.str(), GOSSIP_SENDER_MAIN, i, "Are you sure?", 0, false);
        }
        SendGossipMenuFor(player, DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
    return true;
}

    static bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
{
    player->PlayerTalkClass->ClearMenus();
    if (sender == GOSSIP_SENDER_MAIN && action < tokensSize)
    {
        if (player->HasItemCount(Tokens[action].TAKE_ENTRY, Tokens[action].TAKE_AMOUNT))
        {
            if (player->AddItem(Tokens[action].GIVE_ENTRY, Tokens[action].GIVE_AMOUNT))
            {
                player->DestroyItemCount(Tokens[action].TAKE_ENTRY, Tokens[action].TAKE_AMOUNT, true);
                player->GetSession()->SendAreaTriggerMessage("%u x %s swapped to %u x %s", Tokens[action].TAKE_AMOUNT, GetName(Tokens[action].TAKE_ENTRY).c_str(), Tokens[action].GIVE_AMOUNT, GetName(Tokens[action].GIVE_ENTRY).c_str());
            }
        }
        else
            player->GetSession()->SendNotification("You need at least %u x %ss", Tokens[action].TAKE_AMOUNT, GetName(Tokens[action].TAKE_ENTRY).c_str());
    }
    OnGossipHello(player, creature);
    return true;
}
};

struct MyAI : public ScriptedAI
{
    MyAI(Creature* creature) : ScriptedAI(creature) { }

        bool GossipHello(Player* player) override
        {
            return OnGossipHello(player, me);
        }

        bool GossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override
        {
            uint32 sender = player->PlayerTalkClass->GetGossipOptionSender(gossipListId);
            uint32 action = player->PlayerTalkClass->GetGossipOptionAction(gossipListId);
            return OnGossipSelect(player, me, sender, action);
        }
};

CreatureAI* GetAI(Creature* creature) const override
{
    return new MyAI(creature);
}
};

void AddSC_Exchanger_npc()
{
    new Exchanger_npc();
}

0 个答案:

没有答案