C++ BunkerBuilder.exe中0x0070C75C处的首次机会异常:0xC0000005:访问冲突写入位置0xCC04
当我试图将变量存储在d.allenderModel中,即我的LoadAllender函数中时,我遇到了这个错误。我已经用'//Error'标记了这行。这段代码以前没有抛出任何类似的错误,我也没有修改这段代码,这使我假设我已经对类定义的头/代码进行了一些修改,我也在下面介绍了这些头/代码。提前感谢您提供的任何建议/帮助 引发错误的函数: .cpp文件中定义类的部分: .h文件中定义类的部分: 您是否在头文件和cpp文件中定义了类驻留者和类模型 你们已经定义了它们的不同之处,至少居住者是这样的:我看到一个浮动运动速度;在头文件版本中,但不在cpp版本中 如果你允许我稍微保守一点,我认为这有点危险 建议:清除cpp文件中的居住者和模型定义,并将头文件包含在其中C++ BunkerBuilder.exe中0x0070C75C处的首次机会异常:0xC0000005:访问冲突写入位置0xCC04,c++,exception,cinder,C++,Exception,Cinder,当我试图将变量存储在d.allenderModel中,即我的LoadAllender函数中时,我遇到了这个错误。我已经用'//Error'标记了这行。这段代码以前没有抛出任何类似的错误,我也没有修改这段代码,这使我假设我已经对类定义的头/代码进行了一些修改,我也在下面介绍了这些头/代码。提前感谢您提供的任何建议/帮助 引发错误的函数: .cpp文件中定义类的部分: .h文件中定义类的部分: 您是否在头文件和cpp文件中定义了类驻留者和类模型 你们已经定义了它们的不同之处,至少居住者是这样的:我看
p、 s:对不起,我的英语不好。这是一个学习英语的绝佳机会。我强烈建议您尽快学习如何正确使用调试器。0xCC04接近0xCCCC,这是一种神奇的调试代码,意味着未初始化的堆栈内存。您可能使用的指针位于堆栈中指针未初始化的对象内。感谢您的注意,我想我已经检查过了。我总是在.cpp文件中定义类,然后将其镜像到.h文件中,我想这是出于习惯。有什么理由这样做/不这样做吗?除了不必打字两次以外?@Zachary D.-不客气。是的,我觉得这是个坏习惯。不仅因为你必须键入两次,而且因为你必须确保键入完全相同的内容两次。continue@Zachary你的问题这是一个很好的例子,即使是IMHO:在两个地方定义类,在一个地方多定义一个元素,你的代码就会去同步。代码中有一部分具有定义,另一部分具有不兼容的定义。因此,举例来说,代码的一部分——知道movementSpeed的部分——尝试读取一个成员超过movementSpeed agility,举例来说,可以读取代码的另一部分不知道movementSpeed的部分在另一个位置写入的数据。
dweller loadDweller()
{
dweller d;
do
{
string temp = strIn;
lineIn();
temp = strIn;
int lNum(currLine);
if(checkOperator(strIn, "fName:")) //first name
{
d.firstName = getRemainder(strIn, "fName:");
}
else if(checkOperator(strIn, "lName:")) //Last name
{
d.lastName = getRemainder(strIn, "lName:");
}
else if(checkOperator(strIn, "pos[")) //Position
{
d.pos = loadCoord2();
}
else if(checkOperator(strIn, "jobType:")) //Job type/job
{
d.jobType = getRemainder(strIn, "jobType");
}
else if(checkOperator(strIn, "jobPos:")) //job position
{
d.machine = numInp(strIn, "jobPos:");
}
else if(checkOperator(strIn, "maxHealth:")) //Maximum health
{
d.maxHealth = numInp(strIn, "maxHealth:");
}
else if(checkOperator(strIn, "currHealth:")) //Current health
{
d.currHealth = numInp(strIn, "currHealth:");
}
else if(checkOperator(strIn, "strength:")) //Strength
{
d.strength = numInp(strIn, "strength:");
}
else if(checkOperator(strIn, "stamina:")) //Stamina
{
d.stamina = numInp(strIn, "stamina:");
}
else if(checkOperator(strIn, "agility:")) //Agility
{
d.agility = numInp(strIn, "agility:");
}
else if(checkOperator(strIn, "intelligence:")) //Intelligence
{
d.intelligence = numInp(strIn, "intelligence:");
}
else if(checkOperator(strIn, "modelRef:"))
{
model mod = masterList::getModelTemplate(numInp(strIn, "modelRef:")); //Ensuring that the value can be returned [when debugging]
d.dwellerModel = model(); //Error //Ensuring that the variable can be set [when debugging] by setting it equal to a constructor of itself
d.dwellerModel = masterList::getModelTemplate(numInp(strIn, "modelRef:"));
}
else if(strIn == "model[") //Model
{
d.dwellerModel = loadModel();
}
else if(checkOperator("Gender:")) //Gender
{
d.setGender(getRemainder("Gender:"));
}
else if(strIn != "]")
{
_DEBUG_ERROR("Unknown operator!");
}
else if((strIn != "]") && file.eof())
{
_DEBUG_ERROR("Unexpected EOF");
failed = true;
//return d;
}
} while(!checkOperator("]") && !file.eof());
strIn = "";
return d;
}
class dweller
{
public:
dweller();
//private: //Make the following variables private once all direct access has been changed over to function access
string firstName;
string lastName;
bool isMale; //true = NPC is male, false = NPC is female
var::coord2 pos; //The NPC's position
//bool hasJob; //If the NPC has a job
string jobType; //What the NPC's job is
int machine; //The vector ID of the machine the NPC is assigned to (in the map vector list)
bool isSelected; //Whether or not this NPC is selected by the player
int maxHealth; //The maximum health of the NPC
int currHealth; //The current health of the NPC
int strength;
int stamina;
int agility;
int intelligence;
vector<item> inventory;
float maxInvVolume; //The maximum volume that the inventory can hold (the sum of the volumes in the inventory must be less than this)
model dwellerModel;
float armLURot; //The current angle that the NPC's upper left arm is at
float armLLRot; //The current angle that the NPC's lower left arm is at
float armRURot; //The current angle that the NPC's upper right arm is at
float armRLRot; //The current angle that the NPC's lower right arm is at
var::coord2 toolPosOnHand; //The coordinates that the tool should be centered at on the 'dominantHand' model_segment
model_segment* dominantHand; //A pointer to the hand that single-handed items will be assigned to
model_segment* nonDominantHand; //A pointer to the hand that the secondary position of two-handed items will be assigned to
model_segment* itemLoc; //A pointer to the model of the item that has been assigned to the dominant hand
bool holdingItem;
item heldItem;
public:
void draw();
void update();
bool setInvVolMax(float _maxVol);
float getInvVolMax();
bool addToInventory(item _item);
bool removeFromInventory(int inventoryID); //Removes the item at 'inventoryID' in the 'inventory' vector
bool equipItem(item _item);
bool equipItem(int inventoryID); //Equips the item at 'inventoryID' in the 'inventory' vector
bool unequipItem();
void setGender(bool isMale);
void setGender(string gender); //Male or Female
void executeTask(string _task);
void executeTask(string _task, var::coord2 _pos);
void executeTask(string _task, var::coord2 _pos, block _target);
void executeTask(string _task, var::coord2 _pos, item _target);
void executeTask(string _task, var::coord2 _pos, dweller _target);
void executeTaskEffect(string _effect);
};
class model
{
public:
model();
model_segment root;
vector<anim_container> animations;
bool runAnimations = true;
void draw(var::coord2 position, float rotation);
void update();
void newAnimation();
void newAnimation(anim_container anim);
void updateAnimation();
void updateAnimation(vector<anim_container> *animations);
void initializeAnimation();
void initializeAnimation(vector<anim_container> *animations);
void debugTree(); //Prints a tree diagram of the model
};
model::model()
{
//app::console();
}
model getModelTemplate(int ID) //Returns the template model from the master list at "ID". Defaults to an empty model class if the supplied ID is not defined.
{
if(ID < models.size())
{
return models[ID];
}
else
{
_DEBUG_ERROR("The requested template does not exist");
return model();
}
}
class dweller
{
public:
dweller();
//private: //Make the following variables private once all direct access has been changed over to function access
string firstName;
string lastName;
bool isMale; //true = NPC is male, false = NPC is female
float movementSpeed;
var::coord2 pos; //The NPC's position
//bool hasJob; //If the NPC has a job
string jobType; //What the NPC's job is
int machine; //The vector ID of the machine the NPC is assigned to (in the map vector list)
bool isSelected; //Whether or not this NPC is selected by the player
int maxHealth; //The maximum health of the NPC
int currHealth; //The current health of the NPC
int strength;
int stamina;
int agility;
int intelligence;
vector<item> inventory;
float maxInvVolume; //The maximum volume that the inventory can hold (the sum of the volumes in the inventory must be less than this)
model dwellerModel;
float armLURot; //The current angle that the NPC's upper left arm is at
float armLLRot; //The current angle that the NPC's lower left arm is at
float armRURot; //The current angle that the NPC's upper right arm is at
float armRLRot; //The current angle that the NPC's lower right arm is at
var::coord2 toolPosOnHand; //The coordinates that the tool should be centered at on the 'dominantHand' model_segment
model_segment* dominantHand; //A pointer to the hand that single-handed items will be assigned to
model_segment* nonDominantHand; //A pointer to the hand that the secondary position of two-handed items will be assigned to
model_segment* itemLoc; //A pointer to the model of the item that has been assigned to the dominant hand
bool holdingItem;
item heldItem;
public:
void draw();
void update();
bool setInvVolMax(float _maxVol);
float getInvVolMax();
bool addToInventory(item _item);
bool removeFromInventory(int inventoryID); //Removes the item at 'inventoryID' in the 'inventory' vector
bool equipItem(item _item);
bool equipItem(int inventoryID); //Equips the item at 'inventoryID' in the 'inventory' vector
bool unequipItem();
void setGender(bool isMale);
void setGender(string gender); //Male or Female
void executeTask(string _task, var::coord2 _pos);
void executeTask(string _task, var::coord2 _pos, block _target);
void executeTask(string _task, var::coord2 _pos, item _target);
//void executeTask(string _task, var::coord2 _pos,
void executeTaskEffect(string _effect);
};
class model
{
public:
model();
model_segment root;
vector<anim_container> animations;
bool runAnimations = true;
void draw(var::coord2 position, float rotation);
void update();
void newAnimation();
void newAnimation(anim_container anim);
void updateAnimation();
void updateAnimation(vector<anim_container> *animations);
void initializeAnimation();
void initializeAnimation(vector<anim_container> *animations);
void debugTree();
};