tags:

views:

59

answers:

2

I am having some trouble getting this simple code to work:

#pragma once
#include <iostream>
#include <string>
using std::string;

class UserController;
#include "UserController.h"
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

EDIT: a UserController is instantiating the CreateUserView:

CreateUserView *_createUserView;

This how the CreateUserView is being instantiated:

void UserController::createUser()
{
    //Init the Create User View
    if(_createUserView == NULL)
    {
        _createUserView = new CreateUserView();
        _createUserView->controller = this;
    }
    _createUserView->showView();
}
+3  A: 

You don't seem the initialize your variable properly:

CreateUserView *_createUserView;

Therefore it is a dangling pointer, not NULL (in C++, with a few exceptions, variables are not initialized automatically to 0). So here

if(_createUserView == NULL)
{
    _createUserView = new CreateUserView();
    _createUserView->controller = this;
}

the if block is not executed, and here

_createUserView->showView();

you get access violation. Initialize your pointer properly to NULL:

CreateUserView *_createUserView = NULL;
Péter Török
How come it broke on getline() and not the cout calls? or even when calling '_createUserView->showView();' ?
Default
Excellent That did it! thanks mateone question though, this pointer is private attribute of a class and it won't let me initialize it in the header file sayiig:"only static const integral data members can be initialized within a class"how do you go on doing that, using a initialization list maybe?thanks again
Bach
@user259789: You should set it in your .cpp file. You should have all your definitions in the .cpp file ( the foo(){ } 's) and the declarations in the header file ( foo(); )
Default
@user259789, if it is a nonstatic member, the best is to initialize it in the constructor initialization list.
Péter Török
@Michael, that is the first statement actually dereferencing the object pointer. Since `showView` is nonvirtual, the call to it is resolved statically by the compiler, i.e. no need to dereference `this`. Also, the cout calls don't dereference any members. Still the call is already undefined behaviour, but some compilers will let you get away with it :-)
Péter Török
great info, I was also wondering how did that even compile. From objc on mac to c++(noob) in vc++ I'm hating every aspect of this assignment I got.
Bach
It compiled because there's no "errors". Most compilers have warning levels you can up in order to have use of uninitialized variables flagged, but it's not strictly an error in C++, merely the dreaded "undefined behavior". A compiler marking this as an error would, sadly, be noncompliant (but likely welcomed).
Nathan Ernst
@Nathan, by "some compilers will let you get away with it" I meant they generate code which does not crash right away at the place of the call, only when the data member is actually dereferenced. Thanks for pointing out the sloppy wording.
Péter Török
+1  A: 

Try changing your declaration of the global:

CreateUserView *_createUserView = NULL;
David Gladfelter