Error C3440 on overloaded operator []



 DEVELOP > c-Plus-Plus > Error C3440 on overloaded operator []

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Dan"
Date: 28 May 2006 07:20:43 AM
Object: Error C3440 on overloaded operator []
Hi. I'm fairly new to C++, and am getting an error when trying to
overload the [] operator.
Here's an example of the problem ...
class CMyClass
{
};
class CMyClassCollection
{
public:
CMyClass* operator [] (unsigned int index) const
{
return my_list [index];
}
private:
vector <CMyClass *> my_list;
};
void CMyCodeClass::do_stuff ()
{
CMyClassCollection my_collection;
... initialise my_collection here
CMyClass *my_class = my_collection [0];
}
The last line of the do_stuff function gives me this compiler error
message ...
error C2440: 'initializing' : cannot convert from 'CMyClass *' to
'CMyCodeClass::CMyClass *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
Any ideas what this means?
Thanks for any help,
- Dan
.

User: "Tomás"

Title: Re: Error C3440 on overloaded operator [] 28 May 2006 09:22:05 AM
Dan posted:

Hi. I'm fairly new to C++, and am getting an error when trying to
overload the [] operator.

The following code compiles without error or warning for me:
#include <vector>
using std::vector;
class CMyClass
{
};
class CMyClassCollection
{
public:
CMyClass* operator [] (unsigned int index) const
{
return my_list [index];
}
private:
vector <CMyClass *> my_list;
};
struct CMyCodeClass {
void do_stuff();
};
void CMyCodeClass::do_stuff ()
{
CMyClassCollection my_collection;
//... initialise my_collection here
CMyClass *my_class = my_collection [0];
}
int main() {}
-Tomás
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER