Calling a function returns error C2662. Help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheMan1
    New Member
    • Mar 2008
    • 19

    Calling a function returns error C2662. Help?

    Hi, I'm making a simple program in C++.
    I have an object named TextReader, with a function GetString(), and I also have another object, TextWriter.
    Now when *TextWriter is called, it needs to return a character. It will get this character from TextReader.GetS tring(). However, I get an error C2662 when I try to implement this.
    Here is my dereference operator function for TextWriter:

    Code:
    char TextWriter::operator*() const
    {
    	string tmpStr;
    	tmpStr += firstChar;	//Convert field firstChar to a 1-char string.
    
    	string returnedStr = txtReader.GetString(tmpStr); //Generates error C2662. txtReader is a TextReader object, and a field of TextWriter.
    
    	return returnedStr[0];
    }
    The error I get is "error C2662: 'TextReader.Get String' : cannot convert 'this' pointer from 'const TextReader' to 'TextReader &'".

    How do I resolve this error?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by TheMan1
    Hi, I'm making a simple program in C++.
    I have an object named TextReader, with a function GetString(), and I also have another object, TextWriter.
    Now when *TextWriter is called, it needs to return a character. It will get this character from TextReader.GetS tring(). However, I get an error C2662 when I try to implement this.
    Here is my dereference operator function for TextWriter:

    Code:
    char TextWriter::operator*() const
    {
    	string tmpStr;
    	tmpStr += firstChar;	//Convert field firstChar to a 1-char string.
    
    	string returnedStr = txtReader.GetString(tmpStr); //Generates error C2662. txtReader is a TextReader object, and a field of TextWriter.
    
    	return returnedStr[0];
    }
    The error I get is "error C2662: 'TextReader.Get String' : cannot convert 'this' pointer from 'const TextReader' to 'TextReader &'".

    How do I resolve this error?

    First you should get the cstring from the string and then return the charcter from it like this.
    [code=cpp]
    return returnedStr.c_s tr()[0];
    [/code]

    Raghuram

    Comment

    Working...