我在else if
表達式中基本上有exp>=level*10
,其中level是變量,10是常數。代碼編譯完全正確,沒有任何錯誤,甚至在編譯之前它甚至工作,直到最近。現在,無論何時編譯和執行,它都會給我一個錯誤,指出「無效空指針」,並且它方便地告訴我,問題來自930行中包含的文件xstring(VS2010將其包含在新項目中)。 有沒有辦法強制編程讀取它作爲乘法運算符而不是空指針? 編輯:這是我的完整代碼,請注意,這是一個原型創建,以確保它將工作,並在我意識到我可以使用派生類爲此完成之前完成。還請注意,我試圖刪除using namespace std
,看看是否是這個問題。或C++:VS2010乘法運算符被誤認爲空指針
#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)));
}
問題出在別的地方。你確定你使用的是正確版本的二進制/源代碼嗎? –
即使沒有代碼,我可以告訴你的解釋和建議的解決方案是不正確的。如果你想真正的幫助,然後張貼真實的代碼。 – john
'level * 10'總是*乘法。它不能是別的。 – fredoverflow