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: ";
getli开发者_StackOverflow社区ne(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();
}
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;
Try changing your declaration of the global:
CreateUserView *_createUserView = NULL;
精彩评论