To explain briefly: Using <fstream>, I write a std::list instance to a .txt file:
#include <fstream>
#include <list>
std::list<Item> list_1; //example list
list_1.push_back(Item(...));
std::ofstream file;
file.open("record.txt", std::ios::trunc);
if (file.is_open()) {
file.write((char*)&list_1, sizeof(std::list<Item>)) << std::endl;
file.close();
}
However, when I read from the same file and assign the data to a std::list instance:
file.open("record.txt", std::ios::in);
if (file.is_open()) {
std::list<Item> list_1;
file.read((char*)&list_1, sizeof(std::list<Item>));
}
It gives me an error when I try to access its elements. This is, however, not my problem. Because std::list stores the pointer to that element, I must store the elements manually, like I did here:
for (auto const& item : list_1) {
file << item.amount << std::endl;
file << item.value << std::endl;
file << item.item_name << std::endl;
file << (char*)&item.type << std::endl;
}
Then I read these values. Use the values to create a new Item instance and store it inside my list. Side note: I can access the size() property of the list_1 from the .txt file because it is a member of std::list<Item> which lives on the stack. So it gets saved by the ofstream.
for (int i = 0; i < list_1.size(); i++) {
int amount = 0;
int value = 0;
std::string item_name;
Item_Type type = item;
file >> amount;
file >> value;
file >> item_name;
file >> (char*)&type;
Item item(amount, value, item_name, type);
main_player_inv.push_back(item);
I expect this to work, because now the std::list should have no uninitialized members, right?
Well, it gives me this error:
this->_Mypair._Myval2._Myhead was 0x228B4050240
This basically means list_1->_Mypair._Myval2._Myhead is a pointer which points to memory out of bounds. The problem is, unlike the element pointers which I can manually save the values of and initialize, I can't access the data of list_1->_Mypair._Myval2._Myhead or edit it manually, as it is a private member. Or, there isn't a way I could find online.
So, I have two questions:
Can I initialize list_1->_Mypair._Myval2._Myhead so that it points to a valid memory?
Is there a way to more easily serialize a std::list and retrieve it's content?
If both of these questions are unanswerable, I would like to talk about what I'm trying to do:
The std::list<Item> is used as a character or an object's inventory. In my project, I want to store the items the player and objects such as containers have in a std::list<Item> instance. I thought this was the most fitting thing to do for an object-oriented Player structure. Here are some classes, for example:
Player class
class Player : public Object {
public:
int level, experience;
double health;
float resistance; // 0.000 - 1.000
std::list<Item> inventory;
public:
Player() :
level(0), experience(0), health(10.0), resistance(0.0f) {};
Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
};
Item class
struct Item {
public:
unsigned int amount, value;
std::string item_name;
Item_Type type; // enum
public:
Item() :
amount(0), value(0), item_name("undefined"), type(item) {};
Item(int amount, int value, std::string item_name, Item_Type type) :
amount(amount), value(value), item_name(item_name), type(type) {};
};
If you know a better way to store player items, items being class instances; or know altogether a better way to do this, please help me.
You can't read/write the raw bytes of a std::list object (or any other non-trivial type), as you would be writing/reading raw pointers and other internal data members that you don't need to concern yourself with.
You must (de)serialize your class's individual data members instead, as you have already discovered.
I would suggest a binary format instead of a textual format, eg:
#include <type_traits>
template <typename T, std::enable_if_t<std::is_scalar<T>::value, bool> = true>
void writeToStream(std::ostream &out, const T &value) {
out.write(reinterpret_cast<const char*>(&value), sizeof(value));
}
template <typename T, std::enable_if_t<std::is_scalar<T>::value, bool> = true>
void readFromStream(std::istream &in, T &value) {
in.read(reinterpret_cast<char*>(&value), sizeof(value));
}
void writeToStream(std::ostream &out, const std::string &value) {
size_t size = value.size();
writeToStream(out, size);
out.write(value.c_str(), size);
}
void readFromStream(std::istream &in, std::string &value) {
size_t size;
readFromStream(in, size);
value.resize(size);
in.read(value.data() /* or: &value[0] */, size);
}
template <typename Container>
void writeToStream(std::ostream &out, const Container &items) {
size_t count = items.size();
writeToStream(out, count);
for(const auto& item : items) {
writeToStream(out, item);
}
}
template <typename Container>
void readFromStream(std::istream &in, Container &items) {
size_t count;
readFromStream(in, count);
items.reserve(count);
for(size_t i = 0; i < count; ++i) {
Container::value_type item;
readFromStream(in, item);
items.push_back(item);
}
}
template<typename Container>
void writeToFile(const std::string &fileName, const Container &items) {
std::ofstream file(fileName, std::ios::binary);
file.exceptions(std::ofstream::failbit);
writeToStream(file, items);
}
template<typename Container>
void readFromFile(const std::string &fileName, Container &items) {
std::ifstream file(fileName, std::ios::binary);
file.exceptions(std::ifstream::failbit);
readFromStream(file, items);
}
struct Item {
public:
unsigned int amount, value;
std::string item_name;
Item_Type type; // enum
public:
Item() :
amount(0), value(0), item_name("undefined"), type(item) {};
Item(int amount, int value, std::string item_name, Item_Type type) :
amount(amount), value(value), item_name(item_name), type(type) {};
void writeTo(std::ostream &out) const {
writeToStream(out, amount);
writeToStream(out, value);
writeToStream(out, item_name);
writeToStream(out, type);
}
void readFrom(std::istream &in) {
readFromStream(in, amount);
readFromStream(in, value);
readFromStream(in, item_name);
readFromStream(in, type);
}
};
void writeToStream(std::ostream &out, const Item &item) {
item.writeTo(out);
}
void readFromStream(std::istream &in, Item &item) {
item.readFrom(in);
}
class Player : public Object {
public:
int level, experience;
double health;
float resistance; // 0.000 - 1.000
std::list<Item> inventory;
public:
Player() :
level(0), experience(0), health(10.0), resistance(0.0f) {};
Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
void writeTo(std::ostream &out) const {
writeToStream(out, level);
writeToStream(out, experience);
writeToStream(out, health);
writeToStream(out, resistance);
writeToStream(out, inventory);
}
void readFrom(std::istream &in) {
readFromStream(in, level);
readFromStream(in, experience);
readFromStream(in, health);
readFromStream(in, resistance);
readFromStream(in, inventory);
}
};
void writeToStream(std::ostream &out, const Player &player) {
player.writeTo(out);
}
void readFromStream(std::istream &in, Player &player) {
player.readFrom(in);
}
#include <fstream>
#include <list>
int main() {
std::list<Item> list_1; //example list
list_1.push_back(Item(...));
writeToFile("record.txt", list_1);
list_1.clear();
readFromFile("record.txt", list_1);
return 0;
}
If you really want a textual file, then use operator<< and operator>> instead, overriding them in your classes, eg:
(feel free to tweak this to use whatever formatting you want...)
#include <limits>
void discardLine(std::istream &in) {
in.ignore(std::numeeric_limits<std::streamsize>::max(), '\n');
}
template<typename CharT, typename Traits>
void streamFailed(std::basic_ios<CharT,Traits> &stream) {
stream.setstate(std::ios_base::failbit);
}
template <typename Container>
std::ostream& operator<<(std::ostream &out, const Container &items) {
out << '[' << items.size() << '\n';
for(const auto& item : items) {
out << item << '\n';
}
out << ']\n';
return out;
}
template <typename Container>
std::istream& operator>>(std::istream &in, Container &items) {
char ch;
in >> ch;
if (ch != '[') {
streamFailed(in);
} else {
size_t count;
in >> count;
discardLine(in);
items.reserve(count);
for(size_t i = 0; i < count; ++i) {
Container::value_type item;
in >> item;
items.push_back(item);
}
in >> ch;
if (ch != '[') {
streamFailed(in);
}
}
}
template<typename Container>
void writeToFile(const std::string &fileName, const Container &items) {
std::ofstream file(fileName, std::ios::binary);
file.exceptions(std::ofstream::failbit);
file << items;
}
template<typename Container>
void readFromFile(const std::string &fileName, Container &items) {
std::ifstream file(fileName, std::ios::binary);
file.exceptions(std::ifstream::failbit);
file >> items;
}
struct Item {
public:
unsigned int amount, value;
std::string item_name;
Item_Type type; // enum
public:
Item() :
amount(0), value(0), item_name("undefined"), type(item) {};
Item(int amount, int value, std::string item_name, Item_Type type) :
amount(amount), value(value), item_name(item_name), type(type) {};
friend std::ostream& operator<<(std::ostream &out, const Item &item) {
out << '(' << item.amount << ' ' << item.value << ' ' << static_cast<int>(item.type) << ' ' << item.item_name << ')';
return out;
}
friend std::istream& operator>>(std::istream &in, Item &item) {
char ch;
in >> ch;
if (ch != '(') {
streamFailed(in);
} else {
int itype;
in >> item.amount >> item.value >> itype;
item.type = static_cast<Item_Type>(itype);
std::getline(in >> std::ws, item_name, ')');
}
return in;
}
};
class Player : public Object {
public:
int level, experience;
double health;
float resistance; // 0.000 - 1.000
std::list<Item> inventory;
public:
Player() :
level(0), experience(0), health(10.0), resistance(0.0f) {};
Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
friend std::ostream& operator<<(std::ostream &out, const Player &player) {
out << '(' << level << ' ' << experience << ' ' health << ' ' << resistance << '\n';
out << inventory;
out << ')';
return out;
}
friend std::istream& operator>>(std::istream &in, Player &player) {
char ch;
in >> ch;
if (ch != '(') {
streamFailed(in);
} else {
in >> player.level >> player.experience >> player.health >> player.resistance >> player.inventory;
in >> ch;
if (ch != ')') {
streamFailed(in);
}
}
return in;
}
};
#include <fstream>
#include <list>
int main() {
std::list<Item> list_1; //example list
list_1.push_back(Item(...));
writeToFile("record.txt", list_1);
list_1.clear();
readFromFile("record.txt", list_1);
return 0;
}
Vehicle header file
#define vehicle_h
#include "date.h"
#include "storable.cpp"
#include <string>
using namespace std;
typedef enum{bike = 1,car = 2,towera = 3} VehicleType;
class Vehicle : public Storable
{
private:
string registrationnumber;
VehicleType type;
int seats;
string companyname;
double pricePerKm;
Date PUCExpirationDate;
public:
Vehicle(
string registrationnumber,
VehicleType type,
int seats,
string companyname,
double pricePerKm,
Date PUCExpirationDate, long recordID);
string getregistrationnumber() const;
VehicleType getVehicleType() const;
string getVehicleTypeName() const;
int getseats() const;
string getcompanyName() const;
double getPricePerKm() const;
Date getPUCExpirationDate() const;
void setPricePerKm(double newPrice);
void display() const;
string toString() const;
void setDataForm (Storable * s);
};
#endif // vehicle
Vehicle
#include "Vehicle.h"
#include "STRING_HELPER.h"
const char DELIMETER =';';
Vehicle :: Vehicle(string registrationnumber,VehicleType type,int seats,
string companyName , double pricePerKm , Date PUCExpirationDate , long recordID=0):Storable(recordID)
{
this->registrationnumber =registrationnumber;
this->type= type;
this->seats = seats;
this-> companyname = companyname;
this->pricePerKm = pricePerKm;
this->PUCExpirationDate = PUCExpirationDate;
}
string Vehicle ::getregistrationnumber() const
{
return this->registrationnumber;
}
VehicleType Vehicle::getVehicleType() const
{
return this->type;
}
int Vehicle::getseats() const
{
return this->seats;
}
string Vehicle::getcompanyName() const
{
return this->companyname;
}
double Vehicle::getPricePerKm() const
{
return this->pricePerKm;
}
Date Vehicle::getPUCExpirationDate() const
{
return this->PUCExpirationDate;
}
void Vehicle::setPricePerKm( double newprice)
{
this->pricePerKm= newprice;
}
string Vehicle::getVehicleTypeName() const
{
switch(this->type)
{
case VehicleType:: bike:
return "bike";
case VehicleType:: car:
return "car";
case VehicleType:: towera:
return "Tower";
default:
return " ";
}
}
void Vehicle::display() const
{
cout<< "Vehicle Details :"<<endl;
cout<< "Registration Number : "<< this->registrationnumber<<endl;
cout<< "Vehicle Type : "<< this->type<<endl;
cout<< "No of seats : "<< this->seats<<endl;
cout<< "Company Name : "<< this->companyname<<endl;
cout<< "Price PER Km : "<< this->pricePerKm<<endl;
cout<< "PUC ExpirationDate : "<< this->PUCExpirationDate.toString()<<endl;
}
string Vehicle::toString() const
{
stringstream ss;
ss<<recordID<< DELIMETER<< registrationnumber<< DELIMETER<< type<< DELIMETER<< seats<< DELIMETER<< companyname<< DELIMETER
<< to_string(pricePerKm)<< DELIMETER<< PUCExpirationDate.toString();
return ss.str();
}
void Vehicle ::setDataForm(Storable *s)
{
Vehicle *v = dynamic_cast<Vehicle *> (s);
if(v)
{
this->registrationnumber= v->registrationnumber;
this->type= v->type;
this->companyname= v->companyname;
this->seats= v->seats;
this->pricePerKm = v->pricePerKm;
this->PUCExpirationDate= v->PUCExpirationDate;
}
}
Template Table header file
#define table_h
#include "storable.cpp"
#include "Vehicle.h"
#include "error.cpp"
#include <vector>
#include<string>
#include <fstream>
using namespace std;
template <class T> class Table {
private:
string fileName;
fstream fileStream;
vector<Storable *> * records = NULL;
T * getRefOfRecord(long recordID) const throw(IOError);
void writeToFile () throw(IOError);
T * addNewRecord (T data) const throw (IOError);
void updateRecord (T updateRecord) const throw (IOError);
public:
Table(string fileName) throw (MemoryError);
~Table();
long getNextRecordId() const;
const T* const getRecordForld(long recordId) const throw(IOError);
friend class Database;
};
#endif // table_h
Template table .cpp
#include "Table.h"
#include<iostream>
using namespace std;
template <class T> Table<T> ::Table(string fileName)
throw(MemoryError)
{
this->fileName = fileName;
this->records = new vector<Storable *> ();
if(!this->records)
{
throw MemoryError();
}
}
template <class T> long Table<T> ::getNextRecordId() const
{
return this->records->size()+1;
}
template <class T> T* Table<T> ::addNewRecord (T record) const throw (IOError)
{
T *newRecord = new T(record);
if(!newRecord)
{
throw new MemoryError();
}
newRecord->recordId = this->getNextRecordId();
this->records->push_back(newRecord);
try
{
this->writeToFile();
}
catch(IOError error)
{
this->records->pop_back();
delete newRecord;
throw;
}
return newRecord;
}
template <class T> void Table<T> ::updateRecord(T updateRecord) const throw(IOError)
{
for(auto & record:*this->records)
{
if(record->getRecordID()== updateRecord.getRecordID())
{
T * ptr = dynamic_cast<T*> (record);
if(ptr)
{
T oldRecord = T(* ptr);
record->setDataFrom(&updateRecord);
try
{
this->writeToFile();
return;
}
catch(IOError error)
{
record->setDataFrom (&oldRecord);
throw;
}
}
}
}
throw MemoryError();
}
template <class T> void Table<T> ::writeToFile() throw (IOError)
{
this->fileStream.open(fileName,ios::out | ios::trunc);
if(!this->fileStream)
{
throw IOError();
}
for(auto & record: *records)
{
fileStream <<record->toString()<<endl;
}
this->fileStream.close();
}
template <class T> const T* const Table<T> ::getRecordForld(long recordId) const throw(IOError)
{
try
{
return this->getRefOfRecord(recordId);
}
catch(IOError)
{
throw;
}
}
template <class T> T* Table<T> ::getRefOfRecord(long recordId) const throw (IOError)
{
for(auto &record :*records)
{
if(record->getRecordID()== recordId)
{
return dynamic_cast<T*> (record);
}
}
throw IOError();
}
template <class T> Table<T> ::~Table()
{
for(auto & record : *this->records)
{
delete dynamic_cast<T*> (record);
}
this->records->clear();
this->records->shrink_to_fit();
delete this->records;
}
the template class working fine with the other class such as user or trip but with vehicle class I am getting this error
give the error invalid abstract parameter type 'Vehicle'
I am stuck in this place two three day ago I terid but didn't find any clue !!
I am a beginner at c++ and I am coding a program that stores data into a BST template class the being another class called log_t that stores the variables, I am trying to use the inorder method from the BST class to reference a vector from main, to input the values into and be able to use it in the main class
my main class
int main(int argc, char* argv[])
{
BST<log_t> l;
string infilename = "";
string filePath = "data\\";
string files[1] = {
filePath + "MetData-31-3a.csv"
//filePath + "Jan20071toDec31abcdefghijklmnopq",
//filePath + "Jan20081toDec31abcdefghijklmnopq",
//filePath + "MetData_Jan01-2010-Jan01-2011-ALL"
};
/// checks if file can open
for (int i = 0; i < 1; i++) {
infilename = files[i];
ifstream infile(infilename);
if (!infile.is_open())
{
cout << "unable to read file" << files[i] << endl;
}
else
{
cout << "reading file" << files[i] << endl;
}
/* parse sensor data csv file */
string tmp;
getline(infile, tmp); // skip the first line
while (getline(infile, tmp))
{
// if successfully parsed then append into vector
log_t logline;
if (ParseLog(tmp, logline))
l.insert(logline);
}
}
cout << " end with reading file" << endl;
/* aggregate/filter logs */
Vector<log_t> vec;
l.inorder(vec);
/* prompt menu */
// this array stores all the menu option callback functions
void(*funs[])(const Vector<log_t> & vec) = { NULL, &option1, &option2, &option3, &option4, &option5,&option6 };
// keep printing menu in loop
while (true)
{
// prompt menu and ask user to select option
int choice = PromptMenu();
// check validity of choice
if (choice < 1 || choice > 6)
{
cout << "invalid choice" << endl;
}
else
{
cout << endl;
// call menu option handler
(funs[choice])(vec);
}
}
system("pause");
return -1;
}
my BST class
#include <string>
#include <iostream>
#include <stream>
#include <iomanip>
#include <stream>
#include "date.h"
#include "times.h"
#include "log_t.h"
#include "Vector.h"
using namespace std;
template <class T>
class BST {
private:
struct Node {
T num;
Node* left;
Node* right;
};
Node* root = NULL;
Node* insert(Node* node, T x);
Node* newnode(T num);
void removeprivate(T num, Node* parent);
T findsmallestprivate(Node* ptr);
void inorderprivate(Node* ptr, void (BST<T>::* FT)(T&), Vector<log_t>const& log);
void postorderprivate(Node* ptr, void (BST<T>::* FT)(T&));
void preorderprivate(Node* ptr, void (BST<T>::* FT)(T&));
//void inorderprivate(Node* ptr);
//void postorderprivate(Node* ptr);
//void preorderprivate(Node* ptr);
void removematch(Node* parent, Node* match, bool left);
public:
void insert(T num);
void remove(T num);
void removerootmatch();
T findsmallest();
void inorder(Vector<log_t>const& log);
void postorder();
void preorder();
void print(T& p) { cout << p << " "; };
};
template <class T>
void BST<T>::inorder(Vector<log_t>const& log) {
inorderprivate(root,print,log);
}
template <class T>
void BST<T>::inorderprivate(Node* ptr, void (BST<T>::* FT)(T&), Vector<log_t>const&
log) {
if (root != NULL)
{
if (ptr->left != NULL)
{
inorderprivate(ptr->left, FT);
}
(this->*FT)(log);
log.Append( ptr->num);
if (ptr->right != NULL)
{
inorderprivate(ptr->right, FT);
}
}
else
{
cout << "tree is empty";
}
}
my log_t class the T type
#pragma once
#ifndef LOG_T_H
#define LOG_T_H
#include <iostream>
#include <stream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <stream>
#include "date.h"
#include "times.h"
#include "BST.h"
class log_t
{
public:
log_t();
log_t(log_t& log);
float gettemp();
float getwind();
float getsolar();
void setwind(float wind);
void setsolar(float rad);
void settemp(float temp);
Date date;
Times time;
private:
float wind_speed;
float solar_radiation;
float air_temperature;
};
log_t::log_t()
{
wind_speed = 0;
solar_radiation = 0;
air_temperature = 0;
}
log_t::log_t(log_t& log) {
wind_speed = log.wind_speed;
solar_radiation = log.solar_radiation;
air_temperature = log.air_temperature;
date.SetDate(log.date.GetDay(), log.date.GetMonth(), log.date.GetYear());
time.SetHour(log.time.GetHour());
time.SetMinute(log.time.GetMinute());
}
float log_t:: gettemp()
{
return air_temperature;
}
float log_t::getwind() {
return wind_speed;
}
float log_t::getsolar() {
return solar_radiation;
}
void log_t::setwind(float wind)
{
wind_speed = wind;
}
void log_t::setsolar(float rad)
{
solar_radiation = rad;
}
void log_t::settemp(float temp)
{
air_temperature = temp;
}
#endif // LOG_T_H
my vector class
#pragma once
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
#include <stream>
#include <string>
template <class T>
class Vector
{
public:
Vector();
Vector(int capacity);
Vector(const Vector& vec);
Vector& operator=(const Vector& vec);
~Vector();
int GetSize() const;
void Expand();
T& GetLast();
void Append(const T& val);
T& operator[](int idx);
const T& operator[](int idx) const;
private:
T* elems;
int capacity;/** < int capacity, stores the size of the array */
int count;
void CopyFrom(const Vector& vec);
};
template <class T>
inline Vector<T>::Vector() : elems(nullptr), capacity(0), count(0)
{
}
template <class T>
inline Vector<T>::Vector(int capacity)
: elems(new T[capacity]()), capacity(capacity), count(0)
{
}
template <class T>
inline Vector<T>::Vector(const Vector& vec)
{
CopyFrom(vec);
}
template <class T>
inline Vector<T>& Vector<T>::operator=(const Vector& vec)
{
if (elems)
delete[] elems;
CopyFrom(vec);
return *this;
}
template <class T>
inline Vector<T>::~Vector()
{
if (elems)
delete[] elems;
}
template <class T>
inline int Vector<T>::GetSize() const
{
return count;
}
template <class T>
inline void Vector<T>::Expand()
{
++count;
if (count > capacity)
if (capacity)
capacity *= 2;
else
capacity = 4;
T* tmp = new T[capacity]();
for (int i = 0; i < count - 1; ++i)
tmp[i] = elems[I];
if (elems)
delete[] elems;
elems = tmp;
}
template <class T>
inline T& Vector<T>::GetLast()
{
return elems[count - 1];
}
template <class T>
inline void Vector<T>::Append(const T& oval)
{
Expand();
GetLast() = val;
}
template <class T>
inline T& Vector<T>::operator[](int idx)
{
return elems[idx];
}
template <class T>
inline const T& Vector<T>::operator[](int idx) const
{
return elems[idx];
}
template <class T>
inline void Vector<T>::CopyFrom(const Vector& vec)
{
elems = new T[vec.capacity]();
capacity = vec.capacity;
count = vec.count;
for (int i = 0; i < count; ++i)
elems[i] = vec.elems[i];
}
#endif //VECTOR_H
the errors that keep showing up are
Severity Code Description Project File Line Suppression State
Error (active) E0147 declaration is incompatible with "void BST::inorder(const Vector<> &log)" (declared at line 241 of "C:\Users\Frances\Documents\A2\A2\BST.h") A2 C:\Users\Frances\Documents\A2\A2\BST.h 241
Severity Code Description Project File Line Suppression State
Error C2065 'log_t': undeclared identifier A2 C:\Users\Frances\Documents\A2\A2\BST.h 33
Severity Code Description Project File Line Suppression State
Error C2923 'Vector': 'log_t' is not a valid template type argument for parameter 'T' A2 C:\Users\Frances\Documents\A2\A2\BST.h 33
could someone help me figure out what it is that's causing this or what I am doing wrong, I have been trying to find an answer for hours and haven't been successful thank you
I have implemented a serializer to send data over network. And I have implemented a system that can deserialize primitive data, string, map(string, string), map(string, float), but the error happens with map(string, int) when the deserialized map is used to fetch the value from key. In the debugger I can see that map receive correct value but when I'm trying to get data, I get an error "std::out_of_range at memory location".
Here is my code
#include <stdint.h>
#include <memory>
#include <string>
#include <map>
#include <algorithm>
#define STREAM_ENDIANNESS 0
#define PLATFORM_ENDIANNESS 0
using namespace std;
class OutputMemoryStream
{
void ReallocBuffer(uint32_t inNewLength)
{
mBuffer = static_cast<char*>(std::realloc(mBuffer, inNewLength));
mCapacity = inNewLength;
}
char* mBuffer = nullptr;
uint32_t mHead;
uint32_t mCapacity;
public:
OutputMemoryStream() : mHead(0) { ReallocBuffer(32); }
~OutputMemoryStream()
{
if (mBuffer) { mBuffer = nullptr; }
}
char* GetBufferPtr() const { return mBuffer; }
uint32_t GetLength() const { return mHead; }
void Write(const void* inData, size_t inByteCount)
{
//make sure we have space...
uint32_t resultHead = mHead + static_cast<uint32_t>(inByteCount);
if (resultHead > mCapacity)
{
ReallocBuffer(std::max(mCapacity * 2, resultHead));
}
//copy into buffer at head
std::memcpy(mBuffer + mHead, inData, inByteCount);
//increment head for next write
mHead = resultHead;
}
template< typename T > void Write(T inData)
{
static_assert(std::is_arithmetic< T >::value || std::is_enum< T >::value, "Generic Write only supports primitive data types");
if (STREAM_ENDIANNESS == PLATFORM_ENDIANNESS)
{
Write(&inData, sizeof(inData));
}
else { }
}
template< typename T >
void Write(const std::map< string, T >& inMap)
{
uint32_t elementCount = inMap.size();
Write(elementCount);
for (std::pair<string, T> element : inMap)
{
Write(element.first);
Write(element.second);
}
}
void Write(const std::string& inString)
{
size_t elementCount = inString.size();
Write(elementCount + 1);
Write(inString.data(), (elementCount + 1) * sizeof(char));
}
};
class InputMemoryStream
{
private:
char* mBuffer;
uint32_t mHead;
uint32_t mCapacity;
public:
InputMemoryStream() {}
InputMemoryStream(char* inBuffer, uint32_t inByteCount) : mBuffer(inBuffer), mCapacity(inByteCount), mHead(0) { }
~InputMemoryStream()
{
if (mBuffer) { mBuffer = nullptr; }
}
uint32_t GetRemainingDataSize() const
{
return mCapacity - mHead;
}
void Read(void* outData, uint32_t inByteCount)
{
uint32_t resultHead = mHead + inByteCount;
if (resultHead > mCapacity)
{
//handle error, no data to read!
//...
}
std::memcpy(outData, mBuffer + mHead, inByteCount);
mHead = resultHead;
}
template< typename T > void Read(T& outData)
{
static_assert(std::is_arithmetic< T >::value || std::is_enum< T >::value, "Generic Read only supports primitive data types");
Read(&outData, sizeof(outData));
}
template<typename T1>
void Read(std::map<string, T1> &mapP)
{
size_t elemenCount;
Read(elemenCount);
for (int i = 0; i < elemenCount; i++)
{
string key; T1 value;
Read(key);
Read(value);
std::pair<string, T1> pair(key, value);
mapP.insert(pair);
}
}
void Read(string &outString)
{
size_t strSize;
Read(strSize);
outString.resize(strSize);
for (int i = 0; i < strSize; i++)
{
Read(&outString[i], 1);
}
}
};
class ServerObject
{
OutputMemoryStream outStream;
InputMemoryStream inStream;
map<std::string, int> mapInt;
public:
ServerObject() {};
ServerObject(char* byteArray, int byteCount)
{
InputMemoryStream inStream(byteArray, byteCount);
Deserialize(inStream);
}
~ServerObject() {};
void Serialize()
{
outStream.Write(mapInt);
}
void Deserialize(InputMemoryStream inStream)
{
inStream.Read(mapInt);
}
OutputMemoryStream GetOutStream()
{
return outStream;
}
int GetInt(string key)
{
return mapInt.at(key);
}
void PutInt(string key, int value)
{
mapInt.insert(std::pair<string, int>(key, value));
}
};
int main()
{
ServerObject * so = new ServerObject();
so->PutInt("test", 10);
so->Serialize();
ServerObject * so1 = new ServerObject(so->GetOutStream().GetBufferPtr(), so->GetOutStream().GetLength());
int i = so1->GetInt("test");
system("pause>NULL");
return 0;
}
Your void Write(const std::string& inString) function of OutputMemoryStream should not store additional byte of buffer for null terminator because std::string will not contain null terminator but if you use c_str(), a null terminator will be included in the return from this method. Don't get confused with the internal structure of the memory. std::string stores the length of the string in its member variable so there is no need of null terminator. The function should be as shown below.
void Write(const std::string& inString)
{
size_t elementCount = inString.size();
Write(elementCount);
Write(inString.data(), elementCount * sizeof(char));
}
I've been pulling my hair out over a certain error that seems to be plaguing my program. I've attempted to search online for cases similar to mine but I can't seem to find a way to apply the other solutions to this problem. My issue is as follows: When I initially open the program it immediately stops responding and crashes. Debugging led me to find the error in question is "0xC0000005: Access violation writing location 0xCCCCCCCC". It seems to be tied to me assigning two attributes (sku_ and name_ ) as '\0'. If I change these values to anything else such as "" or even "\0" the program runs in visual studio, but will fail to compile elsewhere. Could someone help me understand where I am going wrong?
Product.h
#ifndef SICT_Product_H__
#define SICT_Product_H__
#include "general.h"
#include "Streamable.h"
#include <cstring>
namespace sict {
class Product : public Streamable {
char sku_ [MAX_SKU_LEN + 1];
char* name_;
double price_;
bool taxed_;
int quantity_;
int qtyNeeded_;
public:
//Constructors
Product();
Product(const char* sku, const char* name1, bool taxed = true, double price = 0, int qtyNeeded =0);
Product(Product& g);
~Product();
//Putter Functions
void sku(const char* sku) { strcpy(sku_,sku); };
void price(double price) {price_ = price;};
void name(const char* name);
void taxed(bool taxed) { taxed_ = taxed; };
void quantity(int quantity) { quantity_ = quantity; };
void qtyNeeded(int qtyNeeded) { qtyNeeded_ = qtyNeeded; };
//Getter functions
const char* sku() const { return sku_; };
double price() const { return price_; };
const char* name() const { return name_; };
bool taxed() const { return taxed_; };
int quantity() const { return quantity_; };
int qtyNeeded() const { return qtyNeeded_; };
double cost() const;
bool isEmpty() const;
Product& operator=(const Product& );
bool operator==(const char* );
int operator+=(int );
int operator-=(int );
};
double operator+=(double& , const Product& );
std::ostream& operator<<(std::ostream& os, const Product& );
std::istream& operator>>(std::istream& is, Product& );
}
#endif
Product.cpp
#include <iostream>
#include <cstring>
#include "Product.h"
namespace sict {
Product::Product() {
sku_[0] = '\0';
name_[0] = '\0';
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;
}
Product::Product(const char* sku, const char* name1, bool taxed1, double price1, int qtyNeeded1) {
strncpy(sku_, sku, MAX_SKU_LEN);
name(name1);
quantity_ = 0;
taxed(taxed1);
price(price1);
qtyNeeded(qtyNeeded1);
}
double Product::cost() const {
if (taxed_ == true) {
return (price_ * TAX) + price_;
}
else
return price_;
}
bool Product::isEmpty() const{
if (sku_ == nullptr && name_ == nullptr && quantity_ == 0 && price_ == 0 && qtyNeeded_ == 0) {
return true;
}
else
return false;
}
Product::Product(Product& ex) {
sku(ex.sku_);
price(ex.price_);
name(ex.name_);
taxed(ex.taxed_);
quantity(ex.quantity_);
qtyNeeded(ex.qtyNeeded_);
}
Product& Product::operator=(const Product& g) {
sku(g.sku_);
price(g.price_);
name(g.name_);
taxed(g.taxed_);
quantity(g.quantity_);
qtyNeeded(g.qtyNeeded_);
return *this;
}
Product::~Product() {
delete [] name_;
}
void Product::name(const char* name) {
name_ = new char [strlen(name) + 1];
strcpy(name_, name);
}
bool Product::operator==(const char* right) {
if (sku_ == right) {
return true;
}
else
return false;
}
int Product::operator+=(int g) {
quantity_ = quantity_ + g;
return quantity_;
}
int Product::operator-=(int g) {
quantity_ = quantity_ - g;
return quantity_;
}
double operator+=(double& p, const Product& right) {
p = p + (right.cost() * right.quantity());
return p;
}
std::ostream& operator<<(std::ostream& os, const Product& g) {
return g.write(os, true);
}
std::istream& operator>>(std::istream& is, Product& g) {
return g.read(is);
}
}
The General.h file referenced in the header is just a list of constant values such as the "TAX" and "MAX_SKU_LEN" values.
The Streamable header contains pure virtual functions. I will list it here in case it is needed.
Streamable.h
#ifndef SICT__Streamable_H_
#define SICT__Streamable_H_
#include <iostream>
#include <fstream>
#include "Product.h"
namespace sict {
class Streamable {
public:
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
};
}
#endif
Thank you very much in advance.
Product::Product() {
sku_[0] = '\0';
name_[0] = '\0'; // <---- writing via unitialized pointer
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;
}
There's one possible source of your problems. You have defined a char pointer (a dynamic array or a C-string, that is) name_ in your class but you never allocate any memory for it in your constructor, before attempting to record a value via the pointer. Naturally, you get a write access violation.
Before assigning the value to char at index [0] you need to first allocate space for at least one element in your string, e.g. by doing name_ = new char [1]. Alternatively, you may choose to initialize the pointer itself to NULL (or nullptr) and use that to indicate that name_ has not yet been set.