Search Results

Search found 2 results on 1 pages for 'user259789'.

Page 1/1 | 1 

  • c++ passing unknown type to a function and any Class type definition

    - by user259789
    I am trying to create a generic class to write and read Objects to/from file. Called it ActiveRecord class only has one method, which saves the class itself: void ActiveRecord::saveRecord(){ string fileName = "data.dat"; ofstream stream(fileName.c_str(), ios::out); if (!stream) { cerr << "Error opening file: " << fileName << endl; exit(1); } stream.write(reinterpret_cast<const char *> (this), sizeof(ActiveRecord)); stream.close(); } now I'm extending this class with User class: class User : public ActiveRecord { public: User(void); ~User(void); string name; string lastName; }; to create and save the user I would like to do something like: User user = User(); user.name = "John"; user.lastName = "Smith" user.save(); how can I get this ActiveRecord::saveRecord() method to take any object, and class definition so it writes whatever i send it: to look like: void ActiveRecord::saveRecord(foo_instance, FooClass){ string fileName = "data.dat"; ofstream stream(fileName.c_str(), ios::out); if (!stream) { cerr << "Error opening file: " << fileName << endl; exit(1); } stream.write(reinterpret_cast<const char *> (foo_instance), sizeof(FooClass)); stream.close(); } and while we're at it, what is the default Object type in c++. eg. in objective-c it's id in java it's Object in AS3 it's Object what is it in C++??

    Read the article

  • c++ setting string attribute value in class is throwing "Access violation reading location"

    - by user259789
    I am having some trouble getting this simple code to work: class CreateUserView { public: CreateUserView(void); ~CreateUserView(void); UserController* controller; void showView(); string name; string lastname; string address; string email; string dateOfBirth; }; All i need is to set these attributes in the implementation with getline(). CreateUserView::CreateUserView(void) { } void CreateUserView::showView() { cout << endl << " New User" << endl; cout << "--------------------------" << endl; cout << " Name\t\t: "; getline(cin, name); cout << " Lastname\t: "; getline(cin, lastname); cout << " Email\t\t: "; getline(cin, email); cout << " ===============================" << endl; cout << " 1. SAVE 2.CHANGE 3.CANCEL" << endl; cout << " ===============================" << endl; cout << " choice: "; int choice; cin >> choice; cin.ignore(); controller->createUser_choice(choice); } I keep getting this "Access violation reading location" error at this line: getline(cin, name); what's the best way of assigning a value to an std::string attribute of a class? even name = "whatever" is throwing that error!! thanks

    Read the article

1