error c2440 returned trying to compile overloaded [] operator 
Author Message
 error c2440 returned trying to compile overloaded [] operator

I have a class that overloads the [] operator.  The method
declaration is as follows:

string ClassName::operator [](const unsigned int &index)

The method returns a string from an array at
subset 'index'.  I call the function from inside main as
follows: (object is a ClassName object)

cout << object[i].c_str() << "\n";

When I compile, I'm getting error c2440, that it cannot
convert from class 'ClassName' to class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >.  I don't understand this, as i'm
not returning 'this', or a 'ClassName' object.  I'm
returning a string!  I checked msdn, and I haven't done
anything inside my code that is out of the ordinary.  I
even tried modifying the method to just return an integer,
and I still get this message.  Can someone help me?  Thank
you.

Sean Rowe



Sat, 22 Oct 2005 00:35:00 GMT  
 error c2440 returned trying to compile overloaded [] operator

Quote:

>I have a class that overloads the [] operator.  The method
>declaration is as follows:

>string ClassName::operator [](const unsigned int &index)

>The method returns a string from an array at
>subset 'index'.  I call the function from inside main as
>follows: (object is a ClassName object)

>cout << object[i].c_str() << "\n";

>When I compile, I'm getting error c2440, that it cannot
>convert from class 'ClassName' to class
>std::basic_string<char,struct std::char_traits<char>,class
>std::allocator<char> >.  I don't understand this, as i'm
>not returning 'this', or a 'ClassName' object.  I'm
>returning a string!  I checked msdn, and I haven't done
>anything inside my code that is out of the ordinary.  I
>even tried modifying the method to just return an integer,
>and I still get this message.  Can someone help me?  Thank
>you.

I would try to reproduce the problem in a minimal console application. If
the problem doesn't become apparent in the process, post the code here. I
just don't have enough to go on at present.

I would consider redeclaring your operator[] as follows:

 const string& ClassName::operator[](unsigned int index);

There's no need to pass an int by reference, and it may be much more
efficient to return the string by const reference. Of course, that would
introduce some string lifetime issues you didn't have before.

--
Doug Harrison
Microsoft MVP - Visual C++



Sat, 22 Oct 2005 01:01:08 GMT  
 error c2440 returned trying to compile overloaded [] operator
I duplicated the error with a very simple class.  Here's
the code:

include <exception>
#include <iostream>
#include <string>

using namespace std;

class simple
{
public:
   simple() {
      for (int i = 0; i < 100; i++) {
        char* buffer;
        string tmp = itoa(i, buffer, 10);
        array[i] = "Hello " + tmp + "\n";
      }
   }

   const string& operator[](const unsigned int index)
   {
      try {
         if (100 <= index)
           throw exception("index value out of range");

      }

      catch(exception& e) {
         cout << e.what() << "\n";
      }

      return array[index];
   }

private:
      string array[100];

Quote:
};

void main(void) {
        simple* obj = new simple();

        for (unsigned int i = 0; i < 100; i++) {
                string tmp = obj[i];
                cout << tmp.c_str();
        }

        delete obj;

Quote:
}

Here's the compiler output:
Compiling...
main.cpp
C:\Program Files\Microsoft Visual
Studio\MyProjects\simpleTest\main.cpp(45) : error
C2440: 'initializing' : cannot convert from 'class simple'
to 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >'
        No constructor could take the source type, or
constructor overload resolution was ambiguous
Error executing cl.exe.

Quote:
>-----Original Message-----

>>I have a class that overloads the [] operator.  The
method
>>declaration is as follows:

>>string ClassName::operator [](const unsigned int &index)

>>The method returns a string from an array at
>>subset 'index'.  I call the function from inside main as
>>follows: (object is a ClassName object)

>>cout << object[i].c_str() << "\n";

>>When I compile, I'm getting error c2440, that it cannot
>>convert from class 'ClassName' to class
>>std::basic_string<char,struct

std::char_traits<char>,class

- Show quoted text -

Quote:
>>std::allocator<char> >.  I don't understand this, as i'm
>>not returning 'this', or a 'ClassName' object.  I'm
>>returning a string!  I checked msdn, and I haven't done
>>anything inside my code that is out of the ordinary.  I
>>even tried modifying the method to just return an
integer,
>>and I still get this message.  Can someone help me?  
Thank
>>you.

>I would try to reproduce the problem in a minimal console
application. If
>the problem doesn't become apparent in the process, post
the code here. I
>just don't have enough to go on at present.

>I would consider redeclaring your operator[] as follows:

> const string& ClassName::operator[](unsigned int index);

>There's no need to pass an int by reference, and it may
be much more
>efficient to return the string by const reference. Of
course, that would
>introduce some string lifetime issues you didn't have
before.

>--
>Doug Harrison
>Microsoft MVP - Visual C++
>.



Sat, 22 Oct 2005 01:34:40 GMT  
 error c2440 returned trying to compile overloaded [] operator

Quote:
> include <exception>
> #include <iostream>
> #include <string>

> using namespace std;

> class simple
> {
> public:
>    simple() {
>       for (int i = 0; i < 100; i++) {
> char* buffer;
> string tmp = itoa(i, buffer, 10);
> array[i] = "Hello " + tmp + "\n";
>       }
>    }

>    const string& operator[](const unsigned int index)
>    {
>       try {
>          if (100 <= index)
>    throw exception("index value out of range");

>       }

>       catch(exception& e) {
>          cout << e.what() << "\n";
>       }

>       return array[index];
>    }

> private:
>       string array[100];
> };

> void main(void) {
> simple* obj = new simple();

> for (unsigned int i = 0; i < 100; i++) {
> string tmp = obj[i];

This should be:

    string tmp = (*obj)[i];

Your obj is a pointer, so subscripting that just
applies pointer arithmetic to the obj pointer, not
to the object that it points to.

By explicitly dereferencing obj, you call the
correct operator[] function. In other words,
this line effectively does this:

    string tmp = obj->operator[](i);

Cheers,
Brian

p.s. main() should return an int ;-)



Sat, 22 Oct 2005 01:47:08 GMT  
 error c2440 returned trying to compile overloaded [] operator


Quote:
> I duplicated the error with a very simple class.  Here's
> the code:

> void main(void) {
> simple* obj = new simple();

> for (unsigned int i = 0; i < 100; i++) {
> string tmp = obj[i];

You're doing the [] on a pointer, not an object. Either declare obj as:

simple obj;

or dereference the pointer:

string tmp = (*obj)[i];

Quote:
> cout << tmp.c_str();
> }

> delete obj;
> }

Jay


Sat, 22 Oct 2005 01:45:00 GMT  
 error c2440 returned trying to compile overloaded [] operator

Quote:
> I duplicated the error with a very simple class.  Here's
> the code:

> class simple
> {
> public:
>    const string& operator[](const unsigned int index)
>    {
>    }
> };

> void main(void) {
> simple* obj = new simple();

> for (unsigned int i = 0; i < 100; i++) {
> string tmp = obj[i];
> cout << tmp.c_str();
> }

Contrary to your original statement, obj is not of type simple - it is
of type pointer to simple. You are not applying overloaded
simple::operator[]. You are applying plain vanilla built-in array access
operator to a pointer to simple, trying to access i-th element of an
array containing just one element. obj[i] is of type simple, which
naturally cannot be converted to string.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Sat, 22 Oct 2005 01:49:06 GMT  
 error c2440 returned trying to compile overloaded [] operator

Quote:

>I duplicated the error with a very simple class.  Here's
>the code:

>include <exception>
>#include <iostream>
>#include <string>

>using namespace std;

>class simple
>{
>public:
>   simple() {
>      for (int i = 0; i < 100; i++) {
>    char* buffer;
>    string tmp = itoa(i, buffer, 10);
>    array[i] = "Hello " + tmp + "\n";
>      }
>   }

>   const string& operator[](const unsigned int index)
>   {
>      try {
>         if (100 <= index)
>       throw exception("index value out of range");

>      }

>      catch(exception& e) {
>         cout << e.what() << "\n";
>      }

>      return array[index];
>   }

>private:
>      string array[100];
>};

>void main(void) {
>    simple* obj = new simple();

>    for (unsigned int i = 0; i < 100; i++) {
>            string tmp = obj[i];
>            cout << tmp.c_str();
>    }

>    delete obj;
>}

>Here's the compiler output:
>Compiling...
>main.cpp
>C:\Program Files\Microsoft Visual
>Studio\MyProjects\simpleTest\main.cpp(45) : error
>C2440: 'initializing' : cannot convert from 'class simple'
>to 'class std::basic_string<char,struct
>std::char_traits<char>,class std::allocator<char> >'
>        No constructor could take the source type, or
>constructor overload resolution was ambiguous
>Error executing cl.exe.

Thanks for the sample code! This might be one of those forehead-slapping
kinda moments, so I'll give a little spoiler space...
.
.
.
.
.
.
.
.
.
.
.
.
OK, the problem is with obj[i]. The variable obj is a pointer, so the
brackets apply to it, and the whole thing amounts to an array access
expression equivalent to *(obj+i). To call simple::operator[] through the
"simple" object you allocated, you have to dereference the pointer and use
parens:

 (*obj)[i]

or use the named operator notation:

 obj->operator[](i)

--
Doug Harrison
Microsoft MVP - Visual C++



Sat, 22 Oct 2005 02:04:08 GMT  
 error c2440 returned trying to compile overloaded [] operator
I want to thank everyone for their help with this.  I knew
it would be something glaringly obvious, and I appreciate
your being kind.  =)  Really, thanks.

Quote:
>-----Original Message-----
>I duplicated the error with a very simple class.  Here's
>the code:

>include <exception>
>#include <iostream>
>#include <string>

>using namespace std;

>class simple
>{
>public:
>   simple() {
>      for (int i = 0; i < 100; i++) {
>    char* buffer;
>    string tmp = itoa(i, buffer, 10);
>    array[i] = "Hello " + tmp + "\n";
>      }
>   }

>   const string& operator[](const unsigned int index)
>   {
>      try {
>         if (100 <= index)
>       throw exception("index value out of range");

>      }

>      catch(exception& e) {
>         cout << e.what() << "\n";
>      }

>      return array[index];
>   }

>private:
>      string array[100];
>};

>void main(void) {
>    simple* obj = new simple();

>    for (unsigned int i = 0; i < 100; i++) {
>            string tmp = obj[i];
>            cout << tmp.c_str();
>    }

>    delete obj;
>}

>Here's the compiler output:
>Compiling...
>main.cpp
>C:\Program Files\Microsoft Visual
>Studio\MyProjects\simpleTest\main.cpp(45) : error
>C2440: 'initializing' : cannot convert from 'class
simple'
>to 'class std::basic_string<char,struct
>std::char_traits<char>,class std::allocator<char> >'
>        No constructor could take the source type, or
>constructor overload resolution was ambiguous
>Error executing cl.exe.

>>-----Original Message-----

>>>I have a class that overloads the [] operator.  The
>method
>>>declaration is as follows:

>>>string ClassName::operator [](const unsigned int &index)

>>>The method returns a string from an array at
>>>subset 'index'.  I call the function from inside main
as
>>>follows: (object is a ClassName object)

>>>cout << object[i].c_str() << "\n";

>>>When I compile, I'm getting error c2440, that it cannot
>>>convert from class 'ClassName' to class
>>>std::basic_string<char,struct
>std::char_traits<char>,class
>>>std::allocator<char> >.  I don't understand this, as
i'm
>>>not returning 'this', or a 'ClassName' object.  I'm
>>>returning a string!  I checked msdn, and I haven't done
>>>anything inside my code that is out of the ordinary.  I
>>>even tried modifying the method to just return an
>integer,
>>>and I still get this message.  Can someone help me?  
>Thank
>>>you.

>>I would try to reproduce the problem in a minimal
console
>application. If
>>the problem doesn't become apparent in the process, post
>the code here. I
>>just don't have enough to go on at present.

>>I would consider redeclaring your operator[] as follows:

>> const string& ClassName::operator[](unsigned int index);

>>There's no need to pass an int by reference, and it may
>be much more
>>efficient to return the string by const reference. Of
>course, that would
>>introduce some string lifetime issues you didn't have
>before.

>>--
>>Doug Harrison
>>Microsoft MVP - Visual C++
>>.

>.



Sat, 22 Oct 2005 02:30:59 GMT  
 error c2440 returned trying to compile overloaded [] operator

Quote:

> I have a class that overloads the [] operator.  The method
> declaration is as follows:

> string ClassName::operator [](const unsigned int &index)

  In addition to what the others said, it might
  be worthwhile to point out, that you cannot
  use this operator on 'const' objects:

    string f(const ClassName& obj)
    {
      return obj[1];
    }

  This won't work.

Quote:
> [...]
> Sean Rowe

  Schobi

--

I'm Schobi at suespammers org



Sat, 22 Oct 2005 17:31:47 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. error C2440 when try to compile template class with overloaded operator

2. error C2440 when try to compile template class with overloaded operator

3. Error C2440 when trying to call DialogBox

4. about compile error c2440

5. C2440 compile error

6. CXX0058: Error: Overloaded Operator Not Found

7. operator [] overloading error

8. VC++ 5.0 ambiguity: conversion operator vs overloaded operator?

9. == and != operator (operator overloading)

10. compile errors trying to do #import vbscript.dll

11. error while TRYING to compile

12. Error error C2440: '=' : cannot convert from

 

 
Powered by phpBB® Forum Software