operatoroverloading - []

D

Dhoom@Rock

hello i get debug error when i going to overload [] operator..
i compile the program in VC++ 6.0 and get error DEMAGE: aftr normal
block(#51)..
my program get run and also get output but at the end of clossing
brecket i got this error...

My Program:
include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

#define LIST 10
class array
{
char *str
  • ;
    int a
    • ;
      int b;

      public:
      array()
      {
      for(int i=0;i<LIST;i++)
      {
      str=NULL;
      a=0;
      }

      b=0;
      }
      ~array()
      {
      for(int i=0;i<b;i++)
      delete str;
      }
      int & operator [] (char * st)
      {
      b++;
      for(int i=0;i<b;i++)
      {
      if(str!=NULL)
      if(strcmp(str,st)==0)return a;

      }

      if((str = (char *)malloc(sizeof(st)))==NULL)
      cout<<"allocation error\n";
      strcpy(str,st);
      return a;

      }


      };




      main()
      {
      array a;
      int b;
      a["dharmesh"] = 10;
      a["manish"]=20;
      a["dha"]=30;

      b=a["dha"];


      printf("%d",b);
      b=a["dharmesh"];
      printf("%d",b);

      }
 
O

Obnoxious User

hello i get debug error when i going to overload [] operator..
i compile the program in VC++ 6.0 and get error DEMAGE: aftr normal
block(#51)..
my program get run and also get output but at the end of clossing
brecket i got this error...

My Program:
include <iostream.h>

#include said:
#include <iomanip.h>

#include <stdlib.h>

#include said:
#include <string.h>

#include said:
#include <stdio.h>

#include said:
#include <conio.h>

Non-standard, and not used in your code.
#define LIST 10
class array
{
char *str
  • ;
    int a
    • ;
      int b;

      public:
      array()
      {
      for(int i=0;i<LIST;i++)
      {
      str=NULL;
      a=0;
      }

      b=0;
      }
      ~array()
      {
      for(int i=0;i<b;i++)
      delete str;


    • delete[] str;
      }
      int & operator [] (char * st)
      {

      What if 'st' == NULL?

      Shouldn't 'b' be incremented only if 'st' can't be found, and thus
      allocated?
      for(int i=0;i<b;i++)
      {
      if(str!=NULL)
      if(strcmp(str,st)==0)return a;


      Since 'b' marks the border between allocated and unallocated, the
      check against NULL is unnecessary.

      if(std::strcmp(str,st)==0) {
      return a;
      }
      }

      if((str = (char *)malloc(sizeof(st)))==NULL)


      sizeof doesn't return what you think here, and why use malloc?
      cout<<"allocation error\n";
      strcpy(str,st);


      int len = std::strlen(st) + 1;
      str = new char[len];
      std::strncpy(str,st,len]);
      return a;


      return a[b++];
      }


      };




      main()

      int main()
      {
      array a;
      int b;
      a["dharmesh"] = 10;
      a["manish"]=20;
      a["dha"]=30;

      b=a["dha"];


      printf("%d",b);
      std::cout<<b<<std::endl;

      b=a["dharmesh"];
      printf("%d",b);

      std::cout<<b<<std::endl;

      return EXIT_SUCCESS;
 
I

Ian Collins

Dhoom@Rock said:
hello i get debug error when i going to overload [] operator..
i compile the program in VC++ 6.0 and get error DEMAGE: aftr normal
block(#51)..
my program get run and also get output but at the end of clossing
brecket i got this error...

My Program:
include <iostream.h>

Non-standard header said:
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
Non-standard header (not required).
#define LIST 10

Why not use a const?
class array
{
char *str
  • ;
    int a
    • ;
      int b;

      public:
      array()
      {
      for(int i=0;i<LIST;i++)
      {
      str=NULL;
      a=0;
      }

      b=0;
      }
      ~array()
      {
      for(int i=0;i<b;i++)
      delete str;
      }
      int & operator [] (char * st)


    • Why not const char* or use std string?
      {
      b++;
      for(int i=0;i<b;i++)
      {
      if(str!=NULL)
      if(strcmp(str,st)==0)return a;

      }

      if((str = (char *)malloc(sizeof(st)))==NULL)


      sizeof st is the size of a char*, not what you intended.
      cout<<"allocation error\n";
      strcpy(str,st);


      This writes to unallocated memory.
 
N

Neelesh Bodas

hello i get debug error when i going to overload [] operator..
i compile the program in VC++ 6.0 and get error DEMAGE: aftr normal
block(#51)..
my program get run and also get output but at the end of clossing
brecket i got this error...

My Program:
include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

#define LIST 10

const int LIST = 10; // prefer const over Macros
class array
{
char *str

  • int a
    • ;
      int b;


    • Prefer vectors and strings over arrays and char*
      //eg:
      std::vector said:
      public:
      array()
      {
      for(int i=0;i<LIST;i++)
      {
      str=NULL;
      a=0;
      }

      b=0;

      // use member initailization list instead of assignment in
      constructors
      }
      ~array()
      {
      for(int i=0;i<b;i++)
      delete str;


      Observe: you are freeing memory using "delete"
      }
      int & operator [] (char * st)
      {
      b++;
      for(int i=0;i<b;i++)
      {
      if(str!=NULL)
      if(strcmp(str,st)==0)return a;

      }

      if((str = (char *)malloc(sizeof(st)))==NULL)


      Observe: you are allocating memory using "malloc".
      Malloc and delete never go together : http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.3
      b=a["dha"];

      printf("%d",b);

      // Prefer C++ style IO


      -N
 
D

Dhoom@Rock

hello friends finally i found the fault and nw application run fine
without any error....thanks every one for helps

Now new issue is that my application now can assign only integer type
data ...
like a["dharmesh"]=10 <--

now i want to assign it either integer or char
like a["dharmesh'] = 'D' <--

it means i should have two function overloading declaration.. like
int & operator [] (char * st)
char & operator [] (char * st)

but my application wan't allow me to do so .. WHY ???

and simply give me error: error C2556: 'char &__thiscall
array::eek:perator [](char *)' : overloaded function differs only by
return type from 'int &__thiscall array::eek:perator [](char *)'


Regards,
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

hello friends finally i found the fault and nw application run fine
without any error....thanks every one for helps

Now new issue is that my application now can assign only integer type
data ...
like a["dharmesh"]=10 <--

now i want to assign it either integer or char
like a["dharmesh'] = 'D' <--

it means i should have two function overloading declaration.. like
int & operator [] (char * st)
char & operator [] (char * st)

but my application wan't allow me to do so .. WHY ???

and simply give me error: error C2556: 'char &__thiscall
array::eek:perator [](char *)' : overloaded function differs only by
return type from 'int &__thiscall array::eek:perator [](char *)'

You can not overload a function only by its return-type, they must have
different arguments (after all, how is the compiler supposed to know
which of them you want otherwise?).
 
N

Neelesh Bodas

hello friends finally i found the fault and nw application run fine
without any error....thanks every one for helps

Now new issue is that my application now can assign only integer type
data ...
like a["dharmesh"]=10 <--

now i want to assign it either integer or char
like a["dharmesh'] = 'D' <--

it means i should have two function overloading declaration.. like
int & operator [] (char * st)
char & operator [] (char * st)

but my application wan't allow me to do so .. WHY ???
because C++ doesnot allow overloading of functions solely on the basis
of return type.

An alternative is to overload operator() for one of the two usages:

int & operator [] (char * st);
char & operator() (char * st);

and then you can use this as:

a["dharmesh"]=10;
a("dharmesh") = 'D';


-N
 
R

Ron Natalie

Obnoxious User wrote:
\
if((str = (char *)malloc(sizeof(st)))==NULL)


sizeof doesn't return what you think here, and why use malloc?
cout<<"allocation error\n";
strcpy(str,st);


int len = std::strlen(st) + 1;
str = new char[len];
std::strncpy(str,st,len]);


Why even use new? These are strings. Use the string
class. If you did, then the program would not have
to misimplement destruction (and copy semantics which
it doesn't even attempt).
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Nice -N but what if we want return type string, float etc...

Please quote the text you are replying to, it makes life easier for
everyone.

Use normal functions instead and name the appropriately:

a.getInt("dharmesh") = 10;
a.getChar("dharmesh") = 'D';
a.getFloat("dharmesh") = 1.0f;
....
 
T

Tristan Wibberley

hello friends finally i found the fault and nw application run fine
without any error....thanks every one for helps

Now new issue is that my application now can assign only integer type
data ...
like a["dharmesh"]=10 <--

now i want to assign it either integer or char
like a["dharmesh'] = 'D' <--

it means i should have two function overloading declaration.. like
int & operator [] (char * st)
char & operator [] (char * st)

but my application wan't allow me to do so .. WHY ???

You need to do something like this:


template<typename T, typename ID>
class smart_assignment {
T* that_;
ID id;
public:
explicit smart_assignment (T* that_, const ID& id_)
: that(that_), id(id_) {}

template<typename U>
T& operator= (const U& v) { that->assign(id, v); return *T; }
};

class foo {
public:
smart_assignment<foo> operator[] (char*st) {
return smart_assignment<foo, char*>(this, st);
}

void assign(char* st, int i) { ... }
void assign(char* st, char c) { ... }
};

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.
 
D

Dhoom@Rock

hello friends finally i found the fault and nw application run fine
without any error....thanks every one for helps
Now new issue is that my application now can assign only integer type
data ...
like a["dharmesh"]=10 <--
now i want to assign it either integer or char
like a["dharmesh'] = 'D' <--
it means i should have two function overloading declaration.. like
int & operator [] (char * st)
char & operator [] (char * st)
but my application wan't allow me to do so .. WHY ???

You need to do something like this:

template<typename T, typename ID>
class smart_assignment {
T* that_;
ID id;
public:
explicit smart_assignment (T* that_, const ID& id_)
: that(that_), id(id_) {}

template<typename U>
T& operator= (const U& v) { that->assign(id, v); return *T; }

};

class foo {
public:
smart_assignment<foo> operator[] (char*st) {
return smart_assignment<foo, char*>(this, st);
}

void assign(char* st, int i) { ... }
void assign(char* st, char c) { ... }

};

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.- Hide quoted text -

- Show quoted text -



This is your code with some modification as per errors that i get now
this code get run.. i also included some statement but dosent include
here bcz it may confuse u or some one...

Just tell me this when i do like
a["dh"] = 10
then in short function calling is like ==> operator [] --> operator =
--> assign()

now when i want to fetch data from a["dh"] then i will write cout <<
a["dh"] ;
so can u plz tell me what should my changes to fetch data
sucessfully ...

should i have to make function like get() similar to assign()... if
yes then when and how i call it ???

I try but find cunfuse me ...


Template<typename T, typename ID>
class smart_assignment {
T* that;
ID id;
public:
explicit smart_assignment (T* that_, ID& id_)
: that(that_), id(id_) {}


template<typename U>
T& operator = ( U& v) { that->assign(id, v); return *that; }



};


class foo {

public:
smart_assignment<foo,char * > operator[] (char*st) {

return smart_assignment<foo, char *>(this, st);
}

void assign(char* st, int f) { }

void assign(char* st, char c) { }

};
 
T

Tristan Wibberley

hello friends finally i found the fault and nw application run fine
without any error....thanks every one for helps
Now new issue is that my application now can assign only integer type
data ...
like a["dharmesh"]=10 <--
now i want to assign it either integer or char
like a["dharmesh'] = 'D' <--
it means i should have two function overloading declaration.. like
int & operator [] (char * st)
char & operator [] (char * st)
but my application wan't allow me to do so .. WHY ???

You need to do something like this:

template<typename T, typename ID>
class smart_assignment {
T* that_;
ID id;
public:
explicit smart_assignment (T* that_, const ID& id_)
: that(that_), id(id_) {}

template<typename U>
T& operator= (const U& v) { that->assign(id, v); return *T; }

};

class foo {
public:
smart_assignment<foo> operator[] (char*st) {
return smart_assignment<foo, char*>(this, st);
}

void assign(char* st, int i) { ... }
void assign(char* st, char c) { ... }

};

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.- Hide quoted text -

- Show quoted text -



This is your code with some modification as per errors that i get now
this code get run.. i also included some statement but dosent include
here bcz it may confuse u or some one...

Just tell me this when i do like
a["dh"] = 10
then in short function calling is like ==> operator [] --> operator =
--> assign()

now when i want to fetch data from a["dh"] then i will write cout <<
a["dh"] ;
so can u plz tell me what should my changes to fetch data
sucessfully ...

should i have to make function like get() similar to assign()... if
yes then when and how i call it ???

yes, but instead of using "member operator=" as for a["dh"] = 5 you
could use an implicit conversion operator as I've inserted below
I try but find cunfuse me ...


Template<typename T, typename ID>
class smart_assignment {
T* that;
ID id;
public:
explicit smart_assignment (T* that_, ID& id_)
: that(that_), id(id_) {}
template<typename U>
T& operator = ( U& v) { that->assign(id, v); return *that; }



};

// This ought to work in the following cases:

int i;
i = a["dh"];

int i = a["dh"];

some_class_with_implicit_constructor_from_int
obj = std::static_cast<int>(a["dh"]);

/* this can get nasty though, so I suggest you seek an alternative
unless you want to spend some time making it safe.
There are other ways to acheive the same thing but I've only scratched
the surface to show you the conversion operator's existence - you have
to be *very* careful with them, they are extremely sharp.

I feel dirty.
*/

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.
 
J

Jerry Coffin

hello friends finally i found the fault and nw application run fine
without any error....thanks every one for helps

Now new issue is that my application now can assign only integer type
data ...
like a["dharmesh"]=10 <--

now i want to assign it either integer or char
like a["dharmesh'] = 'D' <--

it means i should have two function overloading declaration.. like
int & operator [] (char * st)
char & operator [] (char * st)

but my application wan't allow me to do so .. WHY ???

You've basically defined an array type -- an associative array, so the
subscripts aren't integers, but an array type nonetheless. An array (at
least as it's defined in C++) is homogeneous -- i.e. all the objects
contained in the array are of the same type.

You basically have two possible choices to accomplish what you want: you
can either use some sort of variant type that will hold any of the types
that interest you, or else you can put the types that interest you into
a class hierarchy, and then put pointers to the base class of that
hierarchy into the array. The boost library has a class (boost.any) that
you may find helpful if you decide on the former route.

My own guess is that you really haven't defined your problem very well
-- at least IME, real uses for heterogenenous arrays are quite rare.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top