>cout<
user is a pointer, you need to dereference it first:
cout << *user;
Though bare pointers are so passé. You should prefer smart pointers unless there's good reason to do otherwise:
string name, pass;
cout << "enter name: ";
cin >> name;
cout << "enter password: ";
cin >> pass;
auto_ptr<CGuser> user(new CGuser(name,pass,0));
cout << *user;
Note: auto_ptr is used in the example because you're guaranteed to have it available. However, it's problematic in terms of ownership and has actually been deprecated in the upcoming standard. The replacement in the new standard is unique_ptr. You can also use boost::shared_ptr or tr1::shared_pointer with current compilers.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Why the pointer in the first place? You could manage without it, can't you?
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
wouldn't that be a problem when I allocate a buffer with the size of the class(you know, to send it)?
You need to read up on serialization. Real serialization, not this shallow copy crap.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Well, you don't seem to understand serialization at all if the problem is serializing a pointer rather than the pointed to data. The simplest method of serialization is converting the collective value of an object into a string. For example:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class foo {
string _id;
vector<int> _data;
public:
foo(const string& id, const vector<int>& data);
string to_string();
foo& from_string(const string& s);
};
foo::foo(const string& id, const vector<int>& data)
: _id(id), _data(data)
{}
string foo::to_string()
{
string result(_id + "[");
for (vector<int>::size_type i = 0; i < _data.size(); i++) {
result += boost::lexical_cast<string>(_data[i]);
if (i < _data.size() - 1)
result += ",";
}
return result + "]";
}
foo& foo::from_string(const string& s)
{
// Assuming s is formatted properly for brevity
string::size_type begin = s.find_first_of("[");
string::size_type end;
_id = s.substr(0, begin);
while ((end = s.find_first_of(++begin, ',')) != string::npos) {
string data_field = s.substr(begin, end - begin);
_data.push_back(boost::lexical_cast<int>(data_field));
}
return *this;
}
int main()
{
vector<int> v;
v.push_back(11);
v.push_back(22);
v.push_back(33);
v.push_back(44);
foo f1("test", v);
string s = f1.to_string();
foo f2 = f1.from_string(s);
cout << s << '\n' << f2.to_string() << '\n';
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Examples of real serialization methods or libraries are:
Boost.Serialization
Google's protobuf
JSON
Serialization requires two things: a way to deconstruct an object tree into a buffer from which the tree can be reconstructed (which usually requires some form of type identification), and a way to read and write the buffer that is compact, system-agnostic and easy to parse (e.g. XML, JSON, protobuf, etc.).
mike_2000_17
Posting Virtuoso
2,135 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457