2

我的弱点之一是在 C++ 中有效地使用字符,这是我现在正在尝试做的。我的游戏中有一个玩家类,在玩家类中,我创建了一个显示各种信息的 playerCard 对象。这适用于播放器对象的单个实例(即播放器播放器),但是当我尝试将播放器对象推回向量时,一切都会出错。

基本上,程序会继续运行,但播放器不会渲染到屏幕上。当我退出程序时,当 main 尝试返回 MSG 时出现断点错误。关于断点的评论如下:

    /*
     * If this ASSERT fails, a bad pointer has been passed in. It may be
     * totally bogus, or it may have been allocated from another heap.
     * The pointer MUST come from the 'local' heap.
     */
    _ASSERTE(_CrtIsValidHeapPointer(pUserData));

我已将错误定位到此处

    strcat(nameCard, nameChar);
    strcat(nameCard, genderChar);
    strcat(nameCard, ageChar);
    strcat(nameCard, cashHeldChar);
    strcat(nameCard, productWantedChar);

在 playerCard 类中,因为当我将其注释掉时,我没有收到错误消息。这是完整的 playerCard 类(同样,它很混乱,可能是错误的处理方式,但我正在尝试使用字符/字符串等来解决问题)#include "Headers.h";

class Playercard{

private:

    RECT textbox;
    LPD3DXFONT font;

    std::string nameStr;
    std::string genderStr;
    std::string ageStr;
    std::string cashHeldStr;
    std::string prodWantedStr;

    char nameCard[1000];

public:

    Playercard()
    {
    }

    void load(char* name, bool male, int age, double cash, char* prod)
    {

        if(male)
        {
            genderStr = "Gender: Male\n";
        }
        else
        {
            genderStr = "Gender: Female\n";
        }

        nameStr = "Name: " + static_cast<std::ostringstream*>( &(std::ostringstream() << name))->str() + "\n";
        ageStr = "Age: " + static_cast<std::ostringstream*>( &(std::ostringstream() << age))->str() + "\n";
        cashHeldStr = "Cash Held: " + static_cast<std::ostringstream*>( &(std::ostringstream() << cash))->str() + "\n";
        prodWantedStr = "Product Wanted: " + static_cast<std::ostringstream*>( &(std::ostringstream() << prod))->str() + "\n";

        char * nameChar = new char [nameStr.length()+1];
        char * genderChar = new char [genderStr.length()+1];
        char * ageChar = new char [ageStr.length()+1];
        char * cashHeldChar = new char [cashHeldStr.length()+1];
        char * productWantedChar = new char [prodWantedStr.length()+1];

        strcpy(nameChar, nameStr.c_str());
        strcpy(genderChar, genderStr.c_str());
        strcpy(ageChar, ageStr.c_str());
        strcpy(cashHeldChar, cashHeldStr.c_str());
        strcpy(productWantedChar, prodWantedStr.c_str());

        strcat(nameCard, nameChar);
        strcat(nameCard, genderChar);
        strcat(nameCard, ageChar);
        strcat(nameCard, cashHeldChar);
        strcat(nameCard, productWantedChar);

        diagFile.open("Diag.txt");
        diagFile.write("Test", 100);
        diagFile.close();
    }

    void setUp(int L, int T, int R, int B)
    {
        SetRect(&textbox, L,T,R,B);
    }

    void draw()
    {
        font->DrawTextA(d3dSprite, nameCard, -1, &textbox, DT_LEFT, D3DCOLOR_XRGB(255, 255, 255));
    }

    LPCSTR plCard()
    {
        return nameCard;
    }
};

任何帮助将不胜感激。谢谢你。

4

2 回答 2

2

nameCard的未初始化。将第一个替换为strcatstrcpy或使用零字符串对其进行初始化。

现在,std::string独家使用怎么样?

于 2013-02-03T21:00:58.583 回答
2

您的主要问题是nameCard未初始化。strcat需要一个以 null 结尾的字符串来发挥它的魔力,并且不能保证其中的第一个或任何一个字符nameCard是 null。

但是,C 字符串是不必要的。只是std::string一直使用。更改nameCard为字符串后,我将更load改为(不包括文件写入):

void load(const std::string &name, bool male, int age, double cash, const std::string &prod)
{
    nameStr = "Name: " + name + "\n";
    genderStr = "Gender: " + (male ? "Male" : "Female") + "\n";
    ageStr = "Age: " + std::to_string(age) + "\n";
    cashHeldStr = "Cash Held: " + std::to_string(cash) + "\n";
    prodWantedStr = "Product Wanted: " + prod + "\n";

    nameCard = nameStr + genderStr + ageStr + cashHeldStr + prodWantedStr;
}

我实际上只是创建nameCard一个数据成员,删除其他成员,然后使用它:

nameCard.clear();
nameCard += "Name: " + name + "\n";
//add on other parts

除此之外,使plCard()return astd::string和 in draw(),使用nameCard.c_str()。我希望这能更清楚地说明你可以用字符串做什么。

但是请注意,那std::to_string是 C++11。C++03有两种常见的解决方案:

std::string str = boost::lexical_cast<std::string>(someNumber);

或者

std::ostringstream oss;
oss << someNumber;
std::string str = oss.str();

我发现三行比单行或两行更具可读性。

于 2013-02-03T21:11:38.820 回答