简体   繁体   English

使用std :: vector时发生访问冲突

[英]Access violation when using std::vector

I'm getting the following error: 我收到以下错误:

  • Unhandled exception at 0x012a4bd9 in TBG.exe: 0xC0000005: Access violation reading location 0x0000002c. TBG.exe中0x012a4bd9处未处理的异常:0xC0000005:访问冲突读取位置0x0000002c。

Pointing to the size() method of vector.h. 指向vector.h的size()方法。 It seems to happen when this method is used: 使用此方法时似乎会发生:

void Player::printInventory(){
    if(inventory.size() != 0){
        for(unsigned int i=0; i<inventory.size(); i++){
            cout<<inventory[i] << endl;
        }
    }
}

Full code: 完整代码:

Player.h: Player.h:

#pragma once
#include <vector>
#include <memory>

using namespace std;

class Player
{
private:
    int health;
    string name;
    vector<int> inventory;

public:
    Player(void);
    Player(string);
    ~Player(void);
    void changeHealth(int);
    void addToInventory(int);
    void removeFromInventory(int);
    void printInventory();
};

Player.cpp: Player.cpp:

#include "Player.h"
#include <iostream>
#include <string.h>

Player::Player(void)
{
    health = 20;
}

Player::Player(string newName)
{
    name = newName;
    health = 20;
}

Player::~Player(void)
{
}

void Player::changeHealth(int amount){
    health += amount;
}

/*void Player::addToInventory(int item){
    inventory.push_back(item);
}

void Player::removeFromInventory(int itemID){

    for(unsigned int i=0; i<inventory.size(); i++){
        if(inventory[i] == itemID)
            inventory.erase(inventory.begin()+i);
    }

}*/

void Player::printInventory(){
    if(!inventory.empty()){
        for(unsigned int i=0; i<inventory.size(); i++){
            cout<<inventory[i] << endl;
        }
    }
}

main: 主要:

#include "World.h"
#include "Player.h"
#include <iostream>
#include <memory>

World world;

void main(){

    unique_ptr<Player> player(new Player("Ted"));
    world.setPlayer(move(player));
    int selection = 0, inventoryOption = 0, exitOption = 0;

    do{
        inventoryOption = 0;
        exitOption = inventoryOption + 1;

        cout<< inventoryOption <<". View Inventory"<<endl;
        cout<< exitOption <<". Quit game";

        cin>>selection;

        if(selection == inventoryOption){
            player->printInventory();
        }
        else{
        }


    }while(selection != exitOption);

}

Please excuse the messiness, this code is butchered from previous code which has the same errors. 请原谅这个混乱的地方,此代码是从先前的错误代码中提取的。

You're move ing the unique_ptr so that it no longer points to the new Player , then you're using it: 您要move unique_ptr ,使其不再指向new Player ,然后使用它:

world.setPlayer(move(player));

...

player->printInventory();

Don't use move just to make the code compile; 不要仅仅使用move来使代码编译; use shared_ptr so you can have multiple pointers to the object. 使用shared_ptr以便您可以有多个指向该对象的指针。

使用!inventory.empty()代替stocking.size()!=0。因此,对于代码而言,当您移动unique_ptr时,unique_ptr将被释放,因此它将指向零。

Looks like you are using some null object. 看起来您正在使用一些空对象。 Print the value of this(Player) in the function just before the crash. 在崩溃之前在函数中打印this(Player)的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM