我在exp>=level*10
表达式中基本上有else if
,其中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)));
}
答案 0 :(得分:4)
此程序重现您的问题(使用支持调试的运行时使用MSVC编译时):
#include <string>
#include <iostream>
int main()
{
using namespace std;
string s = 0;
cout << s << endl;
}
问题是使用nullpointer初始化std::string
。
编辑:哦,忘记提及,解决方案 - 根本就不要这样做。
干杯&amp;第h。,