Thread: string pointers

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    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

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    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
    My Website

    "Circular logic is good because it is."

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    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...
    hasafraggin shizigishin oppashigger...

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    string pointers

    This newbie thanks you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Pointers
    By mbuster in forum C Programming
    Replies: 3
    Last Post: 05-28-2008, 09:39 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM