大一:c++ 读取访问权限冲突

pos是一个结构体

struct pos {
    std::string name;
    std::string val;
    int num;
};

poss是一个vector型数组:

std::vector<pos> poss

tem是刚读取的变量:



进行到这一步时没有报错,且变量读取正常:

img

在第一个for循环结束时,报错:读取访问权限冲突

img

部分代码:


void read(int &tem, std::vector<pos> &poss) {

        FILE* use = NULL;

        use = fopen("tem.txt", "r+");
        if (use == NULL) return;
        fread(&tem, sizeof(int), 1, use);
        fclose(use);

        use = fopen("pos.txt", "r+");
        if (use == NULL) return;
        for (int i = 0; i < tem; i++) {
            pos low = {};
            fread(&low, sizeof(pos), 1, use);
            poss.push_back(low);
        }
        
        fclose(use);
        
    }

问题出在std:string 上,这个不是一个内存连续的类型,sizeof不能求得它的大小,你查一下它的内存结构就明白了