dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: My string iterator will not dereference

  1. #1
    Join Date
    Jun 2008
    Posts
    1

    My string iterator will not dereference

    I am working to perfect a program that I did in my c++ class this last spring. I made it work one way, however I was, at the time trying a more elegant solution and could not make it work. I am now trying again and still need a little help.

    I get a run time error that says Expression: string iterator not dereferencable. I will include all my code for reference.

    Code:
    #include <iostream>
    #include <string>
    #include "Integer.h"
    using namespace std;
    
    Integer::Integer()
    {
        
    }
    
    Integer::Integer(int i)
    {
        this->equals(i);
    }
    
    Integer::Integer(Integer *I)
    {
        this->equals(I);   
    }
    
    Integer::Integer(std::string s)
    {
        this->equals(s);
    }
    
    void Integer::equals(std::string s)
    {
        if(isNAN(s))
        {
            this->assign(s);
        }
    }
    
    void Integer::equals(Integer I)
    {
        if(!isNAN(I))
        {
            this->assign(I);
        }
    }
    
    void Integer::equals(int i)
    {
        string s=IntToString(i);
        this->assign(s);
    }
    
    string Integer::IntToString(int intValue) 
    { 
      char *myBuff; 
      string strRetVal; 
    
      // Create a new char array 
      myBuff = new char[100]; 
    
      // Set it to empty 
      memset(myBuff,'\0',100); 
    
      // Convert to string 
      itoa(intValue,myBuff,10); 
    
      // Copy the buffer into the string object 
      strRetVal = myBuff; 
       
      // Delete the buffer 
      delete[] myBuff; 
    
      return(strRetVal); 
    }
    
    
    string Integer::toString()
    {
        string s;
        s.assign(*this);
        return s;
    }
    
    int Integer::toInteger()
    {
        int i;
        i = atoi(this->c_str());
        
        return i;
    }
    
    bool Integer::isNAN(string s)
    {
          
        int len=s.length();
        string::iterator it;
        int count=0;
    
    	it=this->begin();
    
    	//if(s.at(s.begin())=='-' || isdigit(s.begin()))
    	if(*it=='-' || *it=='.' || isdigit(*it))
        {
            for(it=s.begin(); it<s.end(); it++)
    	    {
               if(!isdigit(*it) && *it!='.' && *it!='-')
               {
                   return true;
               }
               else if(*it=='-')
               {
                   return true;
               }
               else if(*it=='.')
               {
                   count++;
               }
            }
    
            if(count>1)
                return true;
        }
        else
            return true;
    
        return false;
        
    }
    
    Integer &Integer::operator =(string s)                                                                                                          
    {
        this->equals(s);
        return *this;
    }
    
    Integer &Integer::operator =(Integer I)
    {
    	this->equals(I);
        return *this;
    }
    
    Integer &Integer::operator =(int i)
    {
        this->equals(i);
        return *this;
    }
    
    Integer &Integer::operator +=(Integer I)
    {
        int tmp = this->toInteger()+I.toInteger();
        this->equals(tmp);
        return *this;
    }
    
    Integer &Integer::operator +=(string s)
    {
        int tmp = this->toInteger()+atoi(s.c_str());
        this->equals(tmp);
        return *this;
    }
    
    Integer &Integer::operator +=(int i)
    {
        int tmp = this->toInteger()+i;
        this->equals(tmp);
        return *this;
    }
    The highlighted line is where the error is pointing to. This code compiles but does not run.

  2. #2
    Join Date
    May 2008
    Posts
    96

    Re: My string iterator will not dereference

    Did you just type this in by hand?
    Or did you cut and paste?
    Because it has some errors...

    I think that the actual problem is up five lines where you say:
    Code:
    it=this->begin();
    That should be
    Code:
    it=s.begin();
    !

    If that isn't the cause of your runtime error (but it should be fixed anyway), the next problem is the condition on your highlighted line.
    Don't ever use < with iterators. It is a bad habit that is probably biting you right now. Use != instead:
    Code:
    for (it = s.begin(); it != s.end(); it++)
    The error message means that the iterator is not pointing to a valid location inside the string s. Make sure you don't have code inside the loop there that is advancing it past the end of the string.

    One of those three should fix your problem.

    Hope this helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)