【问题标题】:myvector.size() return 0 in a only functionmyvector.size() 在唯一函数中返回 0
【发布时间】:2018-11-05 12:29:33
【问题描述】:

_curLevel.size() 是我在类级别创建的私有向量,每当我尝试在 getPosition 函数中运行 .size() 函数时,它返回 0。但在其他函数中,此命令运行良好并返回 13。我发现因为它,我的 for 循环不起作用。 我也复制了 main.cpp 和 level.h。

level.cpp

bool level::loadGame(string file_name){
    string line;
    load.open(file_name);
    if (load.fail()){
        cout << "Failed to Load Saved Game" << endl;
        return false;
    }else{
        while ((getline(load, line))){
            _curLevel.push_back(line);
        }
        cout << _curLevel.size() << endl;
        return true;
    }
}
int level::getPosition(char object,char coor){
    char a;
    cout << _curLevel.size() << endl;
    for (unsigned int y = 0; y < (_curLevel.size()); y++){
        for (unsigned int x = 0; x < (_curLevel[y].size()); x++){
            a = _curLevel[y][x];
            if (object == a){
                switch (coor){
                case 'x':
                    return x;
                case 'y':
                    return y;
                default:
                    return 1;
                }
            }
        }
    }
    return 1;
}

void level::printLevel(){
    for (unsigned int i = 0; i < (_curLevel.size()); i++){
        cout << _curLevel[i] << endl;
    }
    cout << _curLevel.size() << endl;
}

level.h

#pragma once
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class level
{
public:
    level();
    bool loadGame(string file_name);
    int getPosition(char object, char coor);
    /*void saveGame(string file_name);*/
    void printLevel();
    void initLevel();
private:
    vector <string> _curLevel;
    ifstream load;
    ofstream save;
};

main.cpp

#include <iostream>
#include "level.h"
#include "player.h"

using namespace std;

int main(){
    level level;
    level.loadGame("ha.txt");
    level.printLevel();
    player player;

    system("pause");
    return 0;
}

播放器.cpp

#include "player.h"
#include "global.h"
#include <iostream>
#include "level.h"

using namespace std;

player::player()
{
    level level;
    _playeryCoor = level.getPosition(playersymbol,'y');
    _playerxCoor = level.getPosition(playersymbol,'x');
    cout << _playeryCoor << endl;
    cout << _playerxCoor << endl;
    cout << "e";
}

播放器.h

#pragma once
#include "level.h"
#include <iostream>

class player
{
public:
    player();
private:
    int _playeryCoor;
    int _playerxCoor;
};

global.h

namespace
{
    char playersymbol = '@';
}

ha.txt

####################################
#..................................#
#..................................#
#..................................#
#..................................#
#...............@..................#
#..................................#
#..................................#
#..................................#
#..................................#
#..................................#
#..................................#
####################################

【问题讨论】:

  • 你永远不会在这段代码中调用 getPosition。
  • 您需要提供minimal reproducible example。还提供一个最小的ha.txt 示例文件
  • player 的一部分未定义,缺少level::level 构造函数以及您从未调用getPosition 的事实,您的代码没有任何明显错误。你确定你运行的代码就是你编译的代码吗?
  • 通过最小的更改即可编译,代码似乎可以工作:coliru.stacked-crooked.com/a/6f62c18b62d1be17
  • stefaanv 谢谢我把它移到 main.cpp 并且它可以工作,我还尝试在 player.cpp 中创建一个新函数,它也不起作用。无论如何让它工作在 player.cpp 中

标签: c++ vector memory-leaks


【解决方案1】:

错误出现在player的添加代码中:您在player的构造函数中声明了一个本地level。该本地level 中的vector 未填充,因此其大小为零。

一种可能的解决方案是将全局level 作为参数传递给player 的构造函数。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-12-26
  • 2015-12-03
  • 2012-03-09
  • 1970-01-01
  • 2021-12-25
  • 2018-07-15
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多