Thread: string operator[] giving error

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    string operator[] giving error

    In MSVC++ 6.0,
    I have lines similar to :

    Code:
    std::string reallylongstr;
    reallylongstr = passed_parm;
    
    //set counters
    
    	while (!isalpha(reallylongstr[counter]) && (counter <= maxlen))
    				{
    				tempword = tempword + (*p).substr(counter, 1);
    				counter++;
    				std::cout<<tempword;
    				}
    I get "Error: overloaded operator not found" related to "reallylongstr[counter]." I try reallylongstr[2] in my watch window and get the same error.

    Ideas?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What type is counter?

    Does this work?

    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string mystr("Hello World");
        std::string::size_type indx = 2;
    
        std::cout << mystr[indx] << std::endl;
    
        return 0;
    }
    Should output 'l'.
    Last edited by hk_mp5kpdw; 09-06-2005 at 08:24 AM.
    "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

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    i think i have counter defined as either long or int. I'll try size_type and see if that makes a difference for my compiler.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is that a compile error or just a watch window error?

    The watch window doesn't evaluate overloaded operators very well. Try expanding the string and its data members a few times to see the internal data which should have the actual string value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM