我是C ++的初学者,我有这样的代码
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
class Role
{
public:
Role(int r, bool x)
:role(r), active(x)
{}
private:
int role;
bool active;
};
class Test
{
public:
Test(vector<Role> xxxx = {})
:xxx(xxxx)
{}
private:
std::vector<Role> xxx;
};
int main()
{
vector<Role> role = { Role(1,true),Role(2,false),Role(3, true) };
Test test1(role);
fstream dataTransfer("data.txt");
Test test2;
dataTransfer.write(reinterpret_cast<char*>(&test1), sizeof(Test));
dataTransfer.read(reinterpret_cast<char*>(&test2), sizeof(Test)); // error
return 0;
}
此代码导致“读取访问冲突”,但我找不到任何错误。 感谢您的帮助