c++ overload pointer ostream - c++

I am a learning c++ and have a class project due in 5 days. I've spent 4 hours researching how to do this however I have not come up with an answer yet. Save me stack!
Problem. I have a pointer to a class which holds a dynamic array. I need to take that array and save it to a file to retrieve later. Here are my 2 headers and the implementation. I am not writing the code to "save to file" yet as that will be easy once I get around this issue. My problem is it keeps printing the address of the pointer and not the data within.
vehReg.h
class vehReg {
public:
/* STUFF */
};
}
#endif
vehData.h
#include "vehReg.h"
using namespace std;
class vehData {
public:
//CONSTRUCTORS
vehData();
//DECONSTRUCTOR
~vehData();
//METHODS
friend ostream &operator<<( ostream &output, const vehData &v);
private:
typedef unsigned long longType;
typedef std::size_t sizeType;
sizeType used,capacity;
vehReg *data;
};
}
#endif
vehData.cpp
//CONSTRUCTOR
vehData::vehData(){
capacity = 5;
used = 0;
data = new vehReg[capacity];
}
//DECONSTRUCTOR
vehData::~vehData(){
delete []data;
}
/* TRYING TO ACCOMPLISH THIS WITH AN OSTREAM OVERLOAD */
void vehData::saveDataSloppy(){
ofstream myFile;
myFile.open ("database.db");
for(int i=0;i<used;i++){
myFile << data[i].getOwnerName() << "|";
myFile << data[i].getVehicleLicense() << "|";
myFile << data[i].getVehicleMake() << "|";
myFile << data[i].getVehicleModel() << "|";
myFile << data[i].getVehicleYear() << "\n";
}
myFile.close();
}
void vehData::saveData(){
cout << data;
}
ostream &operator<<(ostream &stream, const vehData &v){
stream << v.data;
}
}

v.data is a pointer, so it prints a pointer. How do you want it to
print whatever the pointer points to. With the exception of character
pointers, the << always prints what you give it (formatted in some
way). If you don't want it to print a pointer, give is something else.
Suppose it did dereference the pointer. What should it print: one
vehReg? 20? A pointer has no information concerning the size. If
you'd used std::vector<vehReg> (a much better choice), it would know
the size, but there's still no overload on std::vector, since the
system still doesn't know how you want it formatted (comma separated?
each on a new line?). And you've not told it how to print a vehReg
either.
You apparently understand the idea of how to overload <<. The first
thing you'll have to do is provide an overload for vehReg as well.
And both overloads must be defined in terms of existing overloads:
there's not one for std::vector, and the one for pointer doesn't do
what you want (and couldn't), so you'll have to loop in your << for
vehData and output each element, with whatever separators you decide
on. (If it's each element on its own line, then you can use std::copy
and an ostream_iterator for the loop, but this may be a bit in advance
of what you've learnt so far.) And forward to the << for vehReg for
each vehReg.

v.data is a pointer so it's a memory address.
*v.data is what the pointer is pointing to (which in this case is an integer).
For example,
#include <iostream>
using namespace std;
void main () {
int *ptr;
int var = 5;
ptr = &var;
cout << ptr << endl;
cout << *ptr << endl;
system("pause");
}
First line will print out something like: 0043F930
Second line will print out: 5
This should print out the elements held in the data array.
void vehData::showStructure() const {
for (int i = 0; i < capacity: i++) {
cout << data[i];
}
cout << endl;
}

Related

Serialize C++ object with dynamically allocated attributes

I need to serialize an instance of a class to a binary (or some other non-readable) file. The problem is:
The attributes are pointers, which means I need to serialize the heap memory.
Some of the pointers are function pointers.
I've partially managed to deal with the first one. Still, to me all this code looks unsafe and confusing, and I'm still looking for a better way.
Entity.h
#include <vector>
#include <string>
using namespace std;
class Entity
{
private:
vector<int> field_1 = {1, 23, 356, 4787, 5};
vector<double> field_2 = {1.1, 75.757, 0, 45612};
public:
void serialize(const string& filename);
void deserialize(const string& filename);
void show();
void clear();
};
Entity.cpp
#include <iostream>
#include <fstream>
#include "Entity.h"
void Entity::serialize(const string& filename) {
fstream file(filename, ios::binary | ios::out);
if (!file.is_open()) {
cout << "Failed to open the file" << endl;
return;
}
size_t size = field_1.size();
file.write((const char*)&size, sizeof size); // First I write the size of the array
file.write((const char*)field_1.data(), sizeof *field_1.data() * size); // The array itself
size = field_2.size();
file.write((const char*)&size, sizeof size);
file.write((const char*)field_2.data(), sizeof *field_2.data() * size);
file.close();
}
void Entity::deserialize(const string& filename) {
fstream file(filename, ios::binary | ios::in);
if (!file.is_open()) {
cout << "Failed to open the file" << endl;
return;
}
size_t size;
file.read((char*)&size, sizeof size); // Read the size
field_1.resize(size);
file.read((char*)field_1.data(), sizeof *field_1.data() * size); // Read the data
file.read((char*)&size, sizeof size);
field_2.resize(size);
file.read((char*)field_2.data(), sizeof *field_2.data() * size);
file.close();
}
void Entity::show() {
if (field_1.size()) for (auto i : field_1) cout << i << ' ';
else cout << "field_1 is empty";
cout << endl;
if (field_2.size()) for (auto i : field_2) cout << i << ' ';
else cout << "field_2 is empty";
cout << endl;
}
void Entity::clear() {
field_1.clear();
field_2.clear();
}
Main.cpp
#include "Entity.h"
int main() {
Entity e;
e.show();
e.serialize("data.bin");
e.clear();
e.show();
e.deserialize("data.bin");
e.show();
}
The output
1 23 356 4787 5
1.1 75.757 0 45612
field_1 is empty
field_2 is empty
1 23 356 4787 5
1.1 75.757 0 45612
As the amount of attributes grows, this code grows significantly. Besides, I have to watch the order of reading the data, which may accidentally lead to messing all the data.
As for the second point, I haven't come up with any ideas yet.
What is the best way to serialize an instance of a class with pointer attributes?
As #fredrik points out, there are plenty of libraries to do this for you, or to help you do it. Use them. Or if you won't ..
You serialize what an object represents, not how it is held in memory at any specific time. So if you have a pointer p to an object of type T, you serialize *p -- the thing it points at. When deserializing, you need to know enough about the structure that you're deserializing to, that you allocate memory for a T -- that recreates a pointer -- and then deserialize to the fresh object of type T.
As an example, consider serializing this type:
struct T { int i; char *s; };
(terrible structure; use better names and don't use raw char pointers). To serialize an object o of this type, write out the serialization of the value of o.i, and then .. well, what does o.s represent? In the vast majority of systems, there's no point in writing out "the string s was at memory address 0xffe8deadbeef". So you write out what it represents. Is it a string with only ASCII characters? You could write its length, followed by the string it points to. You could serialize this specific structure with a single call to printf("%d;%d;%s;",...). Deserializing is left as an exercise.
You serialize what an function represents, not how it is held in memory. Function pointers are the same: there is no point in writing down "the function at address -151" because in the vast majority of systems, that address will be different the next time you start the computer, run the program, whatever. Write out something that allows you to reconstruct what the function pointer was. If you are hanging on to std::function objects or lambda's, then you are likely SOL if you have no idea what the function is supposed to be doing or why it's there.

No match for 'operator<<' in std::cout [duplicate]

This question already has answers here:
no match for ‘operator<<’ in ‘std::operator
(6 answers)
Closed 5 years ago.
I am developing gsoap web service where I am retrieving vectors of objects in return of a query. I have two ways to do it: first by simple loop and by iterator. None of them working.
The error is:
error: no match for 'operator<<' in 'std::cout
mPer.MultiplePersons::info.std::vector<_Tp, _Alloc>::at<PersonInfo, std::allocator<PersonInfo> >(((std::vector<PersonInfo>::size_type)i))'
MultiplePersons mPer; // Multiple Person is a class, containing vector<PersonInfo> info
std::vector<PersonInfo>info; // PersonInfo is class having attributes Name, sex, etc.
std::vector<PersonInfo>::iterator it;
cout << "First Name: \t";
cin >> firstname;
if (p.idenGetFirstName(firstname, &mPer) == SOAP_OK) {
// for (int i = 0; i < mPer.info.size(); i++) {
// cout << mPer.info.at(i); //Error
//}
for (it = info.begin(); it != info.end(); ++it) {
cout << *it; // Error
}
} else p.soap_stream_fault(std::cerr);
}
It's obvious that operator overloading operator<< in cout is the problem. I have looked at several problems related to this, but no one helped me out. If someone can provide a concrete example on how to solve it, it would be very appreciated. (Please do not talk in general about it, I am new to C++ and I have spent three days on it searching for solution.)
You need to provide an output stream operator for PersonInfo. Something like this:
struct PersonInfo
{
int age;
std::string name;
};
#include <iostream>
std::ostream& operator<<(std::ostream& o, const PersonInfo& p)
{
return o << p.name << " " << p.age;
}
This operator allows expressions of the type A << B, where A is an std::ostream instance (of which std::cout is one) and B is a PersonInfo instance.
This allows you do do something like this:
#include <iostream>
#include <fstream>
int main()
{
PersonInfo p = ....;
std::cout << p << std::endl; // prints name and age to stdout
// std::ofstream is also an std::ostream,
// so we can write PersonInfos to a file
std::ofstream person_file("persons.txt");
person_file << p << std::endl;
}
which in turn allows you to print the de-referenced iterator.
The result of *it is an L-value of type PersonInfo. The compiler is complaining that there is no operator<< which takes a right-hand side argument of type PersonInfo.
For the code to work, you need to provide such an operator, for example like this:
std::ostream& operator<< (std::ostream &str, const PersonInfo &p)
{
str << "Name: " << p.name << "\nAge: " << p.age << '\n';
return str;
}
The exact implementation of the operator depends on your needs for representing the class in output, of course.
What it's telling you is that there isn't a known wway to cout (console output) the contents of *it.
it is an iterator - think of this like a pointer in a list
the list is info so *it is current item in the info, which is a list of PersonInfo items.
So cout << *it; says output to the console the PersonInfo that it is currently referencing.
But the error message is telling you that the compiler doens't know how PersonInfo should be rendered to the console.
What you need to do is create an operator called << that takes an object that cout is (ostream) and a PersonInfo object and then writes the various bits of the PersonInfo to cout.

Char* not retaining text inside my class - initialization constructor problem?

I have the following class (I've trimmed irrelevant stuff):
class Example
{
private:
char* name;
int value[4];
int numVals;
public:
Example();
Example(char name[], int numVals, int, int, int, int);
~Example();
};
And here is the initialization constructor:
Example::Example(char na[], int vals, int v1, int v2, int v3, int v4)
{
name = new char[strlen(na)+1];
strcpy(name, na);
numVals = vals;
value[0] = v1;
value[1] = v2;
value[2] = v3;
value[3] = v4;
// cout << name; // this DOES print out the correct text
}
In my main(), I have an array of Example class, Example myArray[numRecs]. I then have a loop that uses the initialization constructor to fill the array:
myArray[i] = Example(name, numVals, v[0], v[1], v[2], v[3]);
Everything works as expected, however the name does not retain the characters I put into it. I checked using cout what the value is when it is passed into the constructor, and it was correct! However when I use my Example::Print(), it spits out a random character (the same character for each instance of Example).
Here is the Example::Print()
void Example::Print()
{
int total, avg;
total = avg = 0;
cout << left << setw(20) << name << '\t';
for(int i=0; i<4; i++){
if(i<numVals){
cout << left << setw(8) << value[i];
total += value[i];
} else
cout << left << setw(8) << " ";
}
avg = total/numVals;
cout << left << setw(8) << total <<
left << setw(8) << avg << endl;
}
Any ideas? Thanks!
Oh and also, this is for an assignment. We have been told to keep the name field as a char pointer, not a string. I am confused as to what I should be using for the init constructor (char* name or char name[] or.. is there a difference?)
EDIT: Here is the destructor and default constructor. I do not have a copy constructor yet as the assignment does not call for it and it is not used. I'll go ahead and make one for completeness anyway.
Example::~Example()
{
delete [] name;
}
Example::Example()
{
numVals = 0;
for(int i=0; i<4; i++)
value[i] = -1;
}
You should run your program through a memory debugger to witness the nightmare you've created!
You are using manual memory management in your class, yet you forgot to obey the Rule of Three: You didn't implement the copy constructor, assignment operator and destructor! Thus the temporary does allocate memory, copies the pointer (shallowly), and then probably invalidates the memory when it goes out of scope.
The immediate "fix my code" answer is that you have to implement a proper assignment operator and copy constructor to make a deep copy of the char array.
The "this is C++" answer is not to use pointers, new and arrays, and instead std::string.
An easy fix would be to store pointers to the Examples in the array instead of the actual objects. This way, you don't have to deal with copying (which you haven't implemented).

C++: Why is my vector of structs acting as one struct?

I'm working my way through Accelerated C++ and have decided to mess around with the one of structs that were defined in there. While doing so, I've come across a problem: creating a vector of these structs and modifying the elements in each one seems to modify the elements in all of them.
I realize that this probably means I've initialized all the structs in the vector to a struct at a single memory address, but I used the .push_back() method to insert "dummy" structs in to the vector. I was under the impression that .push_back() pushes a copy of its argument, effectively creating a new struct.
Here is the header for the struct:
#ifndef _STUDENT_INFO__CHAPTER_9_H
#define _STUDENT_INFO__CHAPTER_9_H
#include <string>
#include <iostream>
#include <vector>
class Student_info9{
public:
Student_info9(){homework = new std::vector<double>;};
Student_info9(std::istream& is);
std::string getName() const {return name;};
double getMidterm() const {return midterm;};
double getFinal() const {return final;};
char getPassFail() const {return passFail;};
std::vector<double> *getHw(){return homework;};
void setName(std::string n) {name = n;};
void setMidterm(double m) {midterm = m;};
void setFinal(double f) {final = f;};
private:
std::string name;
double midterm;
double final;
char passFail;
std::vector<double> *homework;
};
#endif /* _STUDENT_INFO__CHAPTER_9_H */
And here is the code that i'm fooling around with (excuse the excessive print statements... the result of some time trying to debug :) ):
vector<Student_info9> did9, didnt9;
bool did_all_hw9(Student_info9& s)
{
vector<double>::const_iterator beginCpy = s.getHw()->begin();
vector<double>::const_iterator endCpy = s.getHw()->end();
return(find(beginCpy, endCpy, 0) == s.getHw()->end());
}
void fill_did_and_didnt9(vector<Student_info9> allRecords)
{
vector<Student_info9>::iterator firstDidnt = partition(allRecords.begin(), allRecords.end(), did_all_hw9);
vector<Student_info9> didcpy(allRecords.begin(), firstDidnt);
did9 = didcpy;
vector<Student_info9> didntcpy(firstDidnt, allRecords.end());
didnt9 = didntcpy;
}
int main(int argc, char** argv) {
vector<Student_info9> students;
Student_info9 record;
for(int i = 0; i < 5; i++)
{
students.push_back(record);
}
for(int i = 0; i < students.size(); i++)
{
students[i].setMidterm(85);
students[i].setFinal(90);
students[i].getHw()->push_back(90);
std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;
students[i].getHw()->push_back(80);
std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;
students[i].getHw()->push_back(70);
std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;
std::cout << "Just pushed back students[" << i << "]'s homework grades" << std::endl;
if(i == 3)
students[i].getHw()->push_back(0);
}
std::cout << "student[3]'s homework vector size is " << students[3].getHw()->size() << std::endl;
for(vector<double>::const_iterator it = students[3].getHw()->begin(); it != students[3].getHw()->end(); it++)
std::cout << *it << " ";
std::cout << std::endl;
std::cout << "students[3] has " << ( ( find(students[3].getHw()->begin(),students[3].getHw()->end(), 0) != students[3].getHw()->end()) ? "atleast one " : "no " )
<< "homework with a grade of 0" << std::endl;
fill_did_and_didnt9(students);
std::cout << "did9's size is: " << did9.size() << std::endl;
std::cout << "didnt9's size is: " << didnt9.size() << std::endl;
}
As you can see by the print statements, it seems that the homework grades are being added only to one Student_info9 object, copies of which seem to be populating the entire vector. I was under the impression that if you were to use consecutive copies of .push_back() on a single object, it would create copies of that object, each with different memory addresses.
I'm not sure if that's the source of the problem, but hopefully someone could point me in the right direction.
Thanks.
When you push a StudentInfo onto the vector, it is indeed copied, so that's not the problem. The problem is the vector containing the homework grades. Since you only store a pointer to that vector in StudentInfo, only the pointer, not the vector, is copied when you copy a StudentInfo. In other words you have many different StudentInfos that all have a pointer to the same homework vector.
To fix this you should define a copy constructor which takes care of copying the homework vector.
Have you learned about the copy constructor yet? If so, think about what is happening with vector<Student_info9> students on push_back().
Specifically, what happens with this pointer.
std::vector<double> *homework;
The line Student_info9 record; constructs a Student_info9 using the first constructor. This first constructor creates a vector and stores a pointer to it as a member variable. You then proceed to add a copy of this Student_info9 to a vector 5 times. Each copy has a pointer to the same vector.
Your StudentInfo9 class contanis a pointer to a std::vector<double>, which means in the default copy constructor (which will be called when you add a StudentInfo9 object to your vector), the pointer itself is copied. That means all of your StudentInfo9 objects have the same homework vector.
Does that make sense? Please refer to http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-PTRS.html for a more in depth look at pointers and copy constructors.

Track/Display Array Index As Part Of Cout (C++)

I have a command line C++ program that lets you enter basic information about a person (ID number, name, age, etc.) and I want to output to a console in the following manner:
-------------------------------------------------------------------
Index ID # First Name Last Name Age
-------------------------------------------------------------------
0 1234 John Smith 25
The person objects are stored in an array of Persons and I've overload the ostream (<<) operator to print out all of the fields like you see. The dashed lines and header come from a displayHdg() function. Anyhow, I have not been able to figure out how to get the proper index value for the array. Ideally, I'd like to generate the indices for each line, but all my attempts have failed. The array is looped through and each object printed in the main() function, and the ostream is overloaded in a person class, so I tried to use global variables as well as static variables, and all of those produce incorrect numbering (i.e. show 0, 1 the first time (for 2 objects), then change to 1, 2 on the next display). Any ideas?
Wouldn't this work? (formatting of ID field ommitted)
vector<Person> v;
for (int i = 0; i < v.size(); ++i)
cout << i + 1 << v[i] << endl;
This starts indexing at 1.
EDIT:
OK now I see what you want. You want to find an element in the vector!
std::vector<person>::iterator p =
std::find(Persons.begin(), Persons.end(), element);
if( p != Persons.end() )
{
std::cout << "index of element is: " << p-Persons.begin();
}
If you have the correct formating, you should be able to do the following:
for(size_t i = 0; i < Persons.size(); ++i)
{
cout << i << '\t' << Persons[i] << endl;
}
I would recommend taking a look at the formatting facilities in brief in this post. Using setw, left, right... manipulators is better than doing it manually.
You need to use "find" algorithms to find exact index of Person object in vector < Person>.
You could use wrapper class to hold index and print it according to your formatting in operator<<:
// wrapper to hold index
template<typename T>
struct Ti
{
Ti( size_t index, const T& t ) : index(index), val(t) {}
size_t index;
const T& val;
};
// you class
struct X
{
friend ostream& operator<<( ostream& out, Ti<X>& t );
protected:
int some_data;
};
// operator<< for X
ostream& operator<<( ostream& out, Ti<X>& t )
{
out << "test " << t.index << " " << t.val.some_data;
return out;
}
int main()
{
vector<X> xxx;
for ( size_t i =0; i < xxx.size(); ++i)
cout << Ti<X>(i+1, xxx[i]) << endl;
}