E2094 'operator+' not implemented in type 'XXX' for arguments of type 'int'

Okay I have a problem;  I am trying to extend a string class that I wrote for a friend so I can use it another project I am  working on.  I have an error that makes absolutely no sense.  Here is the code for the copy constructor:

  String(const String &str)  // for copying into a new string object
  {
    Size = str.Size;
    Data = new char[Size+1];
    for (int i = 0; i<Size; i++)
      Data[i] = str[i];
// recieving the following error on the above line: [BCC32 Error] Mystring.h(33): 
// E2094 'operator+' not implemented in type 'String' for arguments of type 'int'
    Data[Size] = '\0';
  };

Now, I have implemented a String::operator+(const &String) for concatentation, and a String::operator[](int Index) for char access, but why is the compiler trying to use a String::operator+ for type int on that line? Size is a private field of String.
0
David
3/4/2014 6:53:45 PM
embarcadero.cppbuilder.cpp 2803 articles. 0 followers. Follow

4 Replies
995 Views

Similar Articles

[PageSpeed] 54

> {quote:title=David Pratt wrote:}{quote}
> Okay I have a problem;  I am trying to extend a string class that I wrote for a friend so I can use it another project I am  working on.  I have an error that makes absolutely no sense.  Here is the code for the copy constructor:
> 
>   String(const String &str)  // for copying into a new string object
>   {
>     Size = str.Size;
>     Data = new char[Size+1];
>     for (int i = 0; i<Size; i++)
>       Data[i] = str[i];
> // recieving the following error on the above line: [BCC32 Error] Mystring.h(33): 
> // E2094 'operator+' not implemented in type 'String' for arguments of type 'int'
>     Data[Size] = '\0';
>   };
> 
> Now, I have implemented a String::operator+(const &String) for concatentation, and a String::operator[](int Index) for char access, but why is the compiler trying to use a String::operator+ for type int on that line? Size is a private field of String.

Here are links to complete code:  
MyString.h:  http://codepad.org/RYXT42hI
MyString.Cpp: http://codepad.org/cYR5yQBr
0
David
3/4/2014 7:10:32 PM
David wrote:

> Now, I have implemented a ... String::operator[](int Index) for char access
<snip>
> but why is the compiler trying to use a String::operator+ for type int
> on that line?

You did not implement a 'const' version of the '[]' operator.  The compiler 
is not able to call your '[]' operator on the 'const' str parameter, so it 
has to resort to an alternative meaning of '[]' (*str + index), but you do 
not have an '+' operator defined that can take an 'int' as input, hense the 
error message.

You need to define both const and non-const versions of your '[]' operator, 
eg:

{code}
// non-const access, can directly manipulate data
char& operator[](int Index) { return Data[Index]; }

// const access, cannot manipulate data
char operator[](int Index) const { return Data[Index]; }
{code}  

--
Remy Lebeau (TeamB)
0
Remy
3/4/2014 7:44:24 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> David wrote:
> 
> > Now, I have implemented a ... String::operator[](int Index) for char access
> <snip>
> > but why is the compiler trying to use a String::operator+ for type int
> > on that line?
> 
> You did not implement a 'const' version of the '[]' operator.  The compiler 
> is not able to call your '[]' operator on the 'const' str parameter, so it 
> has to resort to an alternative meaning of '[]' (*str + index), but you do 
> not have an '+' operator defined that can take an 'int' as input, hense the 
> error message.
> 
> You need to define both const and non-const versions of your '[]' operator, 
> eg:
> 
> {code}
> // non-const access, can directly manipulate data
> char& operator[](int Index) { return Data[Index]; }
> 
> // const access, cannot manipulate data
> char operator[](int Index) const { return Data[Index]; }
> {code}  
> 
> --
> Remy Lebeau (TeamB)

Thanks Remy.
0
David
3/4/2014 8:10:50 PM
David Pratt <> wrote in news:657085@forums.embarcadero.com:

>> {quote:title=David Pratt wrote:}{quote}
>> Okay I have a problem;  I am trying to extend a string class that I
>> wrote for a friend so I can use it another project I am  working on. 
[...]
> Here are links to complete code:  
> MyString.h:  http://codepad.org/RYXT42hI
> MyString.Cpp: http://codepad.org/cYR5yQBr
> 

why don't you just use std::string???

your code has too many issues:

1. naming
String is not good name
    	especially in global scope (no namespace),
    	in file named mystring.h (no match to class name),
    	in C++Builder (which already have frequently used String typedef)

2. missing 'const's
  	String(char *Str){... should be  String(char *Str) 
    	char *c_str() { ... should be char const* c_str()const 
    	int Length(){... should be int Length()const{
    	etc.

3. returning internally allocated memory, which means possible leak
    	char *Concat(String Str);
    	char *Reverse();

4. actual memory leak (because of 3)
    	String operator+(const String &rhs){return Concat(rhs);};

5. missing operator
    	String& operator=( String const& )  - means that you cannot do: a =b+c;

6. not checking for copying the same object
    	String operator=(String &rhs) cause undefined behavior when used in a =a;

7. incorrect signature/unnecessary copies
    	String operator=(String &rhs) by convention should be String& operator=(String const& )
    	void Copy(String Str) should be void Copy(String const& Str) 

8. using named functions instead of more natural operators
    	void Copy( char* ) shoudl be String& operator=(char const*); 
    	char* Concat( String ) should be String operator+=(String const&);

9. function that almost never used
    	char *Reverse()

etc.

Cheers,
Sergiy
0
Sergiy
3/4/2014 11:55:05 PM
Reply:

Similar Artilces:

''''''''''''''''''''
Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/1.7.2.0 GranParadiso/3.0a1 From URL: http://www.mozilla.org/projects/granparadiso/ Note to readers: Hendrix gives...

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

'''''
Name: mario Email: ramar17atfastwebnetdotit Product: Gran Paradiso Alpha 2 Summary: ''''' Comments: K: Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a2) Gecko/20070206 GranParadiso/3.0a2 ...

table_info('','','','%') cannot return any types
I was recently confirming table_info special cases and discovered the case for getting table_types cannot work. table_info('','','','%') should return a list of table types but it returns a list of empty strings instead: my @types = $h->tables('', '', '', '%'); print "all types:\n", join("xxx\n", @types), "\n"; # should output something like: # "dbo" # "INFORMATION_SCHEMA" # "sys" # and actually outputs: xxx xxx It seems to be down to the following ...

Operator '=' is not defined for type 'Char' and type 'Boolean'.
Hello, In my application when i click the button i'm getting this " Operator '=' is not defined for type 'Char' and type 'Boolean'. " But when i test it locally there is no problem at all!!!! Please tell me why this error is occuring...Thanks SwapnaPlease click "mark as answer" if this post helped you. swapnasamsonvarkey: Hello, In my application when i click the button i'm getting this " Operator '=' is not defined for type 'Char' and type 'Boolean'. " But when i test it lo...

Operator '*' is not defined for type 'DBNull' and type 'Decimal'.
 I am getting an error from the following line   <asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0:C}", Eval("ProductQty") * Eval("Price"))   %>' ></asp:Label> I think I may need to convert/define the Variables but not quite sure how to do it. Any help would be great. it's not a matter of converting...its the fact that the value from the DB is Null... You'll have to wrap an IF check around it or change your query to return 0 instead of Null. If the field ...

Operator '>' is not defined for type 'DBNull' and type 'Integer'.
this is my code I don't get why I'm getting this error SubModel is a number :   Example : 823 If Ds.Tables(0).Rows(0).Item("SubModel") > 0 Then Dim crst As New DataSet SqlStr = "SELECT SubModelName FROM SubModel,AcesData WHERE AcesData.SubModel = SubModel.SubModelID and (SubModelID = ?)" Dim da2 As New Data.OleDb.OleDbDataAdapter(SqlStr, conn) da2.SelectCommand.Parameters.Add("?", Data.OleDb.OleDbType.VarChar).Value = Ds.Tables(0).Rows(0).Item("SubModel")...

Cannot convert type 'type' to 'type'
I am learning all of this, so I am still fresh, so please bear with me :)I have a page that has some txtboxes in it, simple registration form, and I have a button that will save those txt boxes to the db. I am using:- I am using Visual Web Developer 2008 Express Edition- C# When I try to compile the project I get error: Cannot convert type 'System.Web.UI.WebControls' to 'String' This is content of my web.config    <profile> <properties> <add name="FirstName"/> <add name="LasttName"/>...

'do' won't 'do' if '/'
Greetings to All from Au, Have a NetWare Perl 5.8.4 and wanted to tweak File\Spec\NW.pm to try and standardise on '/' separators. If I run a test script (t/uni/lower.t) with an unmodified NW.pm, it calls t/uni/case.pl, that, in case.pl, (when the path separators are '\'), the $file is '..\lib\unicore\To\Lower.pl' and the following code portion works, with $simple getting a returned table: sub casetest { my ($base, $spec, $func) = @_; my $file = File::Spec->catfile(File::Spec->catdir(File::Spec->updir, "lib", &qu...

CS0019: Operator '-' cannot be applied to operands of type 'System.DateTime' and 'int'
here is my error codearrCalendar[(dtmDate.Month - 1), (dtmDate.Date - 1)] = myNotes.Text;   datetime.month is an int not sure what this is not working?Ben DateTime.Date returns a DateTime value. Instead you will need to do this: DateTime m_CurrentDate = DateTime.Date;arrCalendar[(dtmDate.Month - 1), (m_CurrentDate.AddDays(-1)] = myNotes.Text; HTH,RyanRyan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to ask a question ahhh my bad I ment to do day not date. thank you, didn't mean to waste your time!Ben...

value of type 'XXX' cannot be converted to type 'XXX'
 I've had this issue with VS2008 lately that is driving me crazy.  I have a website where I built a class that does all my Web Service interaction.  This class returns instances of Web Service proxies, or list of the same.  In my web page I'll create a temporary variable (Dim - I'm using VB) with the same type that my service class is going to return.  When I assign my variable to the return of the method, I'm getting a message similar to the following"Value of type 'Namespace.Foo' cannot be converted to type 'Namespace.Foo'. ...

'''
Name: L Lachowsky Email: e2brutus_10atyahoodotcom Product: Firefox Summary: ''' Comments: why duddn this surprise me... I change default server from IE to Mozilla, and then i get error messages. well, shud I decide to not get on my computer with Mozilla..or shud I risk security breeches with IE....hmmm at least i can get on the internet with IE Browser Details: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 From URL: http://hendrix.mozilla.org/ Note to readers: Hendrix gives no expectation of a response to this fee...

Operator '==' cannot be applied to operands of type 'string[]' and 'string'
Hi, I have the following C# code and the line - " if(a == "Document Library") break; " - is giving me the error - Operator '==' cannot be applied to operands of type 'string[]' and 'string' I am not very familar with c# and would appreciate any help as to how to get around this. I have checked google but can't see anything covering this. Cheers for all help... string [] a; try { currentFolder = currentFolder.Replace("%2f","/"); currentFolder = currentFolder.Replace("%20"," "); a = curre...

Operator '==' incompatible with operand types 'Int64' and 'Object'
I have the typical gridview control driving a formview control.  Both use LinqDataSources.  I'm using the table's ID field, DelID to find the record to be displayed by the FormView when a record is selected from the GridView.  This gives me the above error.  Why, and how do I fix this?Diane Check the type of the operands you are trying to compare.You will have to cast on your operands. something like intvariable == (int)objectvariable; or if(((object)intvariable).Equals(objectvariable))*** If this post helps you, then Mark this post as Answer *** I'm ...

Web resources about - E2094 'operator+' not implemented in type 'XXX' for arguments of type 'int' - embarcadero.cppbuilder.cpp

Resources last updated: 1/19/2016 9:34:15 PM