Notices
Results 1 to 6 of 6

Thread: C++ pointer to string error

  1. #1 C++ pointer to string error 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    this is a simple question that is quite straightforward.
    i want to cout the second character in a pointed string (assuming only strings bigger than size [2] is passed in)
    here is the code:
    Code:
    
    #include<iostream>
    
    void main(void){
    
        std::string *asd=new std::string;
    
        *asd="asdfg";
    
        std::cout<<*asd[1];
    
    }
    and i have this error:
    Code:
        1    IntelliSense: no operator "*" matches these operands
                operand types are: * std::string    c:\Users\samsung\Documents\Visual Studio 2012\Projects\ConsoleApplication3\ConsoleApplication3\Source.cpp    5    13    ConsoleApplication3
    what's wrong?
    help is appreciated!


    Last edited by RamenNoodles; December 5th, 2013 at 08:33 AM.
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Junior TridentBlue's Avatar
    Join Date
    Jan 2013
    Posts
    207
    I'm bad with C and C++, so I took this on for learning. Here's my code:

    int main ()
    {
    std::string str="abcdef"; //the string
    std::string *pointy =&str; // the pointer to the string
    //get the object pointy is referencing, and call its substr
    //method for the substr at position 2, of length 1
    std::string str2 = (*pointy).substr (2,1);
    std::cout << str2;
    std::cout << '\n';

    return 0;
    }


    Reply With Quote  
     

  4. #3  
    Forum Senior
    Join Date
    Sep 2013
    Posts
    305
    Quote Originally Posted by RamenNoodles View Post
    this is a simple question that is quite straightforward.
    i want to cout the second character in a pointed string (assuming only strings bigger than size [2] is passed in)
    here is the code:
    Code:
    
    #include<iostream>
    
    void main(void){
    
        std::string *asd=new std::string;
    
        *asd="asdfg";
    
        std::cout<<*asd[1];
    
    }
    and i have this error:
    Code:
        1    IntelliSense: no operator "*" matches these operands
                operand types are: * std::string    c:\Users\samsung\Documents\Visual Studio 2012\Projects\ConsoleApplication3\ConsoleApplication3\Source.cpp    5    13    ConsoleApplication3
    what's wrong?
    help is appreciated!
    Did the formatting muck up your code because:
    Code:
    std::string *asd=new std::string;


    certainly isn't going to compile

    Last edited by Trivium; December 5th, 2013 at 03:29 PM.
    Reply With Quote  
     

  5. #4  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    You're confusing C++ strings and C strings. If you're using C++ (and not trying to make something compatible with some old C library), just stick to C++ strings and forget the pointers. (Pointers are still important, but not here.)

    For C++ strings:
    Code:
    std::string str = "asdf";
    std::cout << str[2];
    For C strings (still using C++ new and delete though):
    Code:
    char str[5] = "asdf";
    std::cout << str[2];
    I should mention I haven't tested these, but it's hard to mess the C++ version up.
    Reply With Quote  
     

  6. #5  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    thanks, i get that std::string is actually a class, and how do i access a function in the string object?

    why do i have to use "->" for a non-array string pointer instead of using "." ?

    cant i use mystring.erase(1,2) instead of mystring->erase(1,2)?

    is there a specific reason?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  7. #6  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    If you have a std::string, then you'd use ".". If you've made a pointer to a string you'd use "->" which is actually just a shortcut to "(*ptr)." most of the time. The string class overloads the [] operator to pretend its an array of characters.
    Reply With Quote  
     

Similar Threads

  1. Error
    By JVogel in forum General Discussion
    Replies: 2
    Last Post: July 4th, 2012, 11:10 AM
  2. Replies: 4
    Last Post: October 24th, 2011, 10:30 AM
  3. Error
    By rrw4rusty in forum Pseudoscience
    Replies: 10
    Last Post: March 1st, 2010, 03:40 PM
  4. Mouse pointer freezes
    By leohopkins in forum Computer Science
    Replies: 5
    Last Post: January 28th, 2008, 12:59 PM
  5. error correction
    By Meta-El in forum Mathematics
    Replies: 1
    Last Post: June 18th, 2005, 07:05 AM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •