473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Calling a function returns error C2662. Help?

TheMan1
19
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.GetString(). However, I get an error C2662 when I try to implement this.
Here is my dereference operator function for TextWriter:

Expand|Select|Wrap|Line Numbers
  1. char TextWriter::operator*() const
  2. {
  3.     string tmpStr;
  4.     tmpStr += firstChar;    //Convert field firstChar to a 1-char string.
  5.  
  6.     string returnedStr = txtReader.GetString(tmpStr); //Generates error C2662. txtReader is a TextReader object, and a field of TextWriter.
  7.  
  8.     return returnedStr[0];
  9. }
The error I get is "error C2662: 'TextReader.GetString' : cannot convert 'this' pointer from 'const TextReader' to 'TextReader &'".

How do I resolve this error?
Mar 22 '08 #1
1 1884
gpraghuram
1,275 Expert 1GB
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.GetString(). However, I get an error C2662 when I try to implement this.
Here is my dereference operator function for TextWriter:

Expand|Select|Wrap|Line Numbers
  1. char TextWriter::operator*() const
  2. {
  3.     string tmpStr;
  4.     tmpStr += firstChar;    //Convert field firstChar to a 1-char string.
  5.  
  6.     string returnedStr = txtReader.GetString(tmpStr); //Generates error C2662. txtReader is a TextReader object, and a field of TextWriter.
  7.  
  8.     return returnedStr[0];
  9. }
The error I get is "error C2662: 'TextReader.GetString' : 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.
Expand|Select|Wrap|Line Numbers
  1.     return returnedStr.c_str()[0];
  2.  
Raghuram
Mar 23 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
3
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
13
by: ukrbend | last post by:
I'm new to Javascript and to html and am trying to make the following code snippet work but it doesn't. It refuses to call the getPage() function and I always get a 404 error. I know the code is...
4
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias...
1
by: netguy | last post by:
I'm coding a VB.NET application that calls a C dll. The VB.NET application make a call to C dll to establish a session and inturn the C dll returns a pointer to pointer. function declarations...
2
by: ramasubramanian.rahul | last post by:
hi i am trying to call some java APIs from c . i use the standatd JNI calls to load the JVM from a c program and call all java functions by using a pointer to the jvm which was returned by the JNI...
2
by: Qasimtn | last post by:
Error occured while compiling the function which is a operator(<) function if remove the key word const in the argument no error occurs. Help me i have no idea what i have done wrong with this...
3
by: mishink7 | last post by:
i am getting this error error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &' with and ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.