或 C++:VS2010 乘法运算符被误认为空指针

发布于 2024-11-30 21:04:01 字数 2943 浏览 0 评论 0原文

我基本上在 else if 表达式中使用 exp>=level*10 ,其中 level 是变量,10 是常量。代码编译完全正常,没有任何错误,甚至在编译后仍然可以工作,直到最近。现在,每当它编译和执行时,它都会给我一个错误,说“无效的空指针”,它很方便地告诉我问题来自第 930 行包含的文件 xstring (VS2010 将其包含在新项目中)。 有没有办法强制编程将其读取为乘法运算符而不是空指针? 编辑:这是我的完整代码,请注意,这是一个创建的原型,以确保它能够工作,并且是在我意识到我可以为此使用派生类之前完成的。另请注意,我尝试删除 using namespace std 以查看这是否是问题所在。

#include "stdafx.h"
#include <iostream>
#include <sstream>

class player {
public:
    std::string name;
    int hp;
    int attack;
    int strength;
    int defense;
    void reward(int gold, int exp) {
        player::gold += gold; 
        player::exp += exp;
        player::levelHandler();
    }
    int dataQuery(std::string query) {
        if (query == "equipment")
            return helm, armour, greaves;
        else if (query == "gold")
            return gold;
        else if (query == "exp")
            return exp;
        else if (query == "level")
            return level;
        else return 0;
    }
private:
    int helm;
    int armour;
    int greaves;
    int level;
    int gold;
    int exp;
    std::string levelHandler() {
        if (level==0) { 
            level=1;
        }
        else if (exp>=(10*level)) { //causes problem due to * being mistaken as a 
                    //null pointer.
        int previousLevel = level;
        level += 1;
        levelHandler();
        return 0;
        };
        return 0;
    };
} hero;

class enemy {
public:
    std::string name;
    int hp;
    int attack;
    int strength;
    int defense;
private:
    int helm;
    int armour;
    int greaves;
    int level;
    int goldReward;
    int expReward;
    int dataQuery(std::string query) {
        if (query == "equipment")
            return helm, armour, greaves;
        else if (query == "gold")
            return goldReward;
        else if (query == "exp")
            return expReward;
        else if (query == "level")
            return level;
        else return 0;
    }
};

int main()
{
    std::cout << "Please enter your character's name..." << std::endl;
    std::cin >> hero.name;
    std::cout << "Welcome to RPG Battle Simulation, " << hero.name << "!" << std::endl;
    hero.reward(100, 0); //problem is either this line, or the function's call to levelHandler, as this is where the program terminates.
    std::cout << "You are beginning your journey as a level " << hero.dataQuery("level") << " character." << std::endl;
    std::cout << "Here is " << hero.dataQuery("gold") << " gold to get you started." << std::endl;
    return 0;
};

以下是导致问题的 xstring 的摘录。问题来自 _DEBUG_POINTER(_Ptr); (第 930 行)

_Myt& assign(const _Elem *_Ptr)
        {// assign [_Ptr, <null>)
 _DEBUG_POINTER(_Ptr);
 return (assign(_Ptr, _Traits::length(_Ptr)));
       }

I basically have exp>=level*10 in an else if expression, where level is a variable and 10 is a constant number. The code compiles completely fine without any errors, and it even worked after being compiled, until recently. now, whenever it compiles and executes, it gives me an error saying "invalid null pointer" and it conveniently tells me that the problem comes from the included file xstring (VS2010 includes it in new projects) on line 930.
Is there a way to force to program to read it as the multiplication operator instead of a null pointer?
EDIT: Here is my full code, please note that this is a protype created to ensure that it will work and was done prior to me realizing I can use derived classes for this. also note that I tried removing using namespace std to see if that was the problem.

#include "stdafx.h"
#include <iostream>
#include <sstream>

class player {
public:
    std::string name;
    int hp;
    int attack;
    int strength;
    int defense;
    void reward(int gold, int exp) {
        player::gold += gold; 
        player::exp += exp;
        player::levelHandler();
    }
    int dataQuery(std::string query) {
        if (query == "equipment")
            return helm, armour, greaves;
        else if (query == "gold")
            return gold;
        else if (query == "exp")
            return exp;
        else if (query == "level")
            return level;
        else return 0;
    }
private:
    int helm;
    int armour;
    int greaves;
    int level;
    int gold;
    int exp;
    std::string levelHandler() {
        if (level==0) { 
            level=1;
        }
        else if (exp>=(10*level)) { //causes problem due to * being mistaken as a 
                    //null pointer.
        int previousLevel = level;
        level += 1;
        levelHandler();
        return 0;
        };
        return 0;
    };
} hero;

class enemy {
public:
    std::string name;
    int hp;
    int attack;
    int strength;
    int defense;
private:
    int helm;
    int armour;
    int greaves;
    int level;
    int goldReward;
    int expReward;
    int dataQuery(std::string query) {
        if (query == "equipment")
            return helm, armour, greaves;
        else if (query == "gold")
            return goldReward;
        else if (query == "exp")
            return expReward;
        else if (query == "level")
            return level;
        else return 0;
    }
};

int main()
{
    std::cout << "Please enter your character's name..." << std::endl;
    std::cin >> hero.name;
    std::cout << "Welcome to RPG Battle Simulation, " << hero.name << "!" << std::endl;
    hero.reward(100, 0); //problem is either this line, or the function's call to levelHandler, as this is where the program terminates.
    std::cout << "You are beginning your journey as a level " << hero.dataQuery("level") << " character." << std::endl;
    std::cout << "Here is " << hero.dataQuery("gold") << " gold to get you started." << std::endl;
    return 0;
};

The following is an excerpt of xstring that is causing the problem. the problem comes from _DEBUG_POINTER(_Ptr); (line 930)

_Myt& assign(const _Elem *_Ptr)
        {// assign [_Ptr, <null>)
 _DEBUG_POINTER(_Ptr);
 return (assign(_Ptr, _Traits::length(_Ptr)));
       }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2024-12-07 21:04:01

该程序重现了您的问题(当使用支持调试的运行时的 MSVC 进行编译时):

#include <string>
#include <iostream>

int main()
{
    using namespace std;

    string  s = 0;
    cout << s << endl;
}

问题是 std::string 是用空指针初始化的。

编辑:哦,忘了提及,解决方案 - 只是不要这样做。

干杯&呵呵,

This program reproduces your problem (when compiled with MSVC with runtime that supports debugging):

#include <string>
#include <iostream>

int main()
{
    using namespace std;

    string  s = 0;
    cout << s << endl;
}

The problem is that a std::string is initialized with a nullpointer.

EDIT: oh, forgot to mention, solution – simply don’t do that.

Cheers & hth.,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文