Problem with string*

This is a discussion on Problem with string* within the C++ Programming forums, part of the General Programming Boards category; okay, I've got a class with two pointers to strings, but I can't figure out how to call the length() ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    46

    Problem with string*

    okay, I've got a class with two pointers to strings, but I can't figure out how to call the length() function of the default string class (it exists doesn't it?). here's what I've been trying to do:

    Code:
    typedef string* strptr;
    
    class Book
    {
      public:
        //big 3
        ...
        ...
        Book(const Book& b1);
        //other functions, not important right now
        ...
      private:
        strptr title;
        strptr author;
        ...
    };
    
    //copy constuctor for class Book
    Book::Book(const Book& b1)
    {
      strptr t, a;
    
      t = b1.title;
      a = b1.author;
    
      title = new string[t.length()];
      author = new string[a.length()]; 
      ...
      ...
    }
    I'm compiling with g++ and I keep getting a syntax error that says:

    error: 'length' has not been declared
    error: request for member of non-aggregate type before '(' token

    I also tried it with:
    Code:
    title = new string[t&.length()];
    author = new string[a&.length()];
    but that gives me this message:

    error: expected primary-expression before '.' token

    what am I doing wrong?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    try with
    Code:
    t->length();
    a->length();
    Before you can access anything that a pointer points to you need to dereference the pointer, doing *somepointer.
    After that you can use the . to access functions and membervariables. So, t->length() is the same as (*t).length().

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    thanks, it's compiling now, but now I'm getting a segmentation fault...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Your copy constructor should probably look closer to this:

    Code:
    Book::Book(const Book& b1)
    {
        title = new string(*b1.title);
        author = new string(*b1.author); 
        ...
        ...
    }
    That should create and initialize the new string pointers all in one go.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    That can be because of many reasons but my guess would be that you are trying to access memory that you shouldnt access (or maybe you are trying to access a NULL-pointer).

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    yeah, that's what I have now, I'm new to using the string class so I wasn't sure how it worked

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    title = new string[t->length()];

    this means title would be an array of strings on the heap with as many strings as the length of t. Is that really what you want?

    And why the typedef? With strings of the STL string class the need for pointers is really limited.

    Why not do something simple like this (code meant as general way to go---not tested/compiled) and be rid of all the pointer/new hassle?
    Code:
    class Book
    {
       public:
    	 Book(const Book &);
    	 //etc.
       private:
    	 string title;
    	 string author;
    };
     
    Book::Book(const Book & b1)
    {
       title = b1.title;
       author = b1.author;
    }
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21