-
string pointers
Hi,
I'm trying to use string pointers within a class, but when I run de-bugger, I get an access violation from the line where I initialize the object. Here is a shortened version of my class:
class Employee
{
string *sName;
string *sNumber;
public:
Employee()
{
*sName = "Doe, John"; // access violation
*sNumber = "0000"; // This would be too, but
} // don't get this far.
};
Thanks,
David
-
you have to allocate memory for the strings before you can assign values...ex:
string *myString; //declare point to a string
myString = new string; //allocate memory in heap
*myString = "Hello, World!"; //assign
-
you need to allocated memory to the pointer... also, after it's declaration, you cannot store the string into the memory allocated into the pointer like that, you'd have to do it 'by hand'... accessing and assigning each element of the array...
-
string pointers
This newbie thanks you! :)