I was trying to call a member of class a from class b but was unsuccessful it works but always gives garbage value for the vector in class a and yes the exception is thrown on the third time while the read loop is only supposed to run only 2 times.
b.h
#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
#include "a.h"
using namespace std;
class b {
fstream fio;
string s;
a aio;
public:
b(string);
b( b& );
void all(string);
void out();
void reset();
~b();
};
b.cpp
#include "b.h"
b::b(string in)
:fio{ "see.bin",ios::in | ios::out | ios::binary | ios::app }, s{ in }
{
}
b::b( b& bi )
: fio{ "see.bin",ios::in | ios::out | ios::binary | ios::app }, s{ bi.s }
{
}
void b::all(string in) {
s = in;
reset();
aio.set_n( s ); //n is a string in a class
aio.set_s( 11 ); // s is a size_t object in a class
aio.set_v( 114 ); // v is the vector in a class
fio.write( ( char* )&aio, sizeof( aio ) );
reset();
}
void b::out() {
reset();
while (fio.read( ( char* )&aio, sizeof( aio ) //read function gone rouge
aio.print();
}
void b::reset() {
fio.clear();
fio.seekg( 0, ios_base::beg );
}
b::~b() {
fio.close();
}
main.cpp
#include "b.h"
int main() {
b obj_1( "" );
obj_1.all( "xyz" );
obj_1.out();
}
a.cpp
#include "a.h"
a::a()
:s{ 0 }
{
}
a::a( a& ai )
: s{ ai.s }, n{ ai.n }
{
for (auto c : ai.v)
v.push_back( c );
}
a::a( a&& ai ) noexcept
:s{ ai.s }, n{ ai.n }
{
for (auto c : ai.v)
v.push_back( c );
ai.v.clear();
}
void a::set_v( int i ) {v.push_back( i );}
void a::set_s( size_t si ) { s = si;}
void a::set_n( string na ) {n = na;}
void a::print(){
cout << s << " " << n << " ";
for (int &p : v)
cout << " " << p;
}
a::~a() {v.clear();}
for reference a is a class having a copy constructor, move constructor ,normal constructor ,all the setters for each object and an destructor. Thank you for helping me and reading my badly written code.