473,327 Members | 1,920 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,327 software developers and data experts.

overloading assignment operator

I have code for the overloading assignment like this:

Expand|Select|Wrap|Line Numbers
  1. Stack& Stack::operator = (const Stack& rightOp)
  2.    {
  3.    if( this != &rightOp )
  4.      {
  5.      delete[] arraystack;
  6.      arraysize = rightOp.arraysize;
  7.      capacity = rightOp.capacity;
  8.      arraystack = new int;
  9.      strcpy( arraystack, rightOp.arraystack);
  10.      }
  11.    return *this;
  12.    }
The logic is:

1. Check for self-assignment. If the address stored in the pointer this is the same as the address of the object rightOp, then skip to the final step.
2. Delete the string array for the object that called the method.
3. Set the string size for the object that called the method to the string size of rightOp.
4. Set the string capacity for the object that called the method to the string capacity of rightOp.
5. Use the string array pointer for the object that called the method to allocate an array of char. The size of the new string array should be the string capacity plus one.
6. Copy the contents of the string array of rightOp into the string array of the object that called the method.
7. Return *this.

The problem is when compile this code there is warning regarding the strcpy.
Anythig wrong with my code, please advise.Thank you
Oct 23 '11 #1

✓ answered by weaknessforcats

Your arraystack is an array of int. strcpy only copies C-style strings.

You will need to copy each element of rightOP.arraystack to
this->arraystack. Use a loop that runs fron 0 to rightOP.arraysize.

4 2888
weaknessforcats
9,208 Expert Mod 8TB
You need to allocate memory for this->arraystack before you strcpy into it.

Use rightOp.arraysize to do that.
Oct 24 '11 #2
Stack& Stack::operator = (const Stack& rightOp)
{
if( this != &rightOp )
{
delete[] arraystack;
arraysize = rightOp.arraysize;
capacity = rightOp.capacity;
arraystack = new int[capacity];
strcpy( arraystack, rightOp.arraystack);
}
return *this;
}

Thank you for the advise, but how can I allocate memory for this->arraystack Please help.
When I compile the above code, I got the following error:

Stack.cpp: In copy constructor Stack::Stack(const Stack&):
Stack.cpp:54: error: cannot convert int* to char* for argument 1 to char* strcpy(char*, const char*)

Since my private data members are:

int* arraystack;
int capacity;
int arraysize;
int arraytop;
Oct 25 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
Your arraystack is an array of int. strcpy only copies C-style strings.

You will need to copy each element of rightOP.arraystack to
this->arraystack. Use a loop that runs fron 0 to rightOP.arraysize.
Oct 25 '11 #4
Thank you very much. I got it now. You are the best!!!!
Oct 26 '11 #5

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

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
3
by: jim.brown | last post by:
The attached code implements a test class to show an error in overloading operator=. This code works on Windows with Visual Studio and simpler cases work with gcc 3.3.2 on Solaris 9. On Windows,...
3
by: Abubakar | last post by:
Hi, lets say I have a class called "hashstring". I want to be able to write the following code: hashstring hs ( "hello world" ); char * somecharptr; somecharptr = hs; // here the somecharptr...
12
by: Achim Domma | last post by:
Hi, I want to use Python to script some formulas in my application. The user should be able to write something like A = B * C where A,B,C are instances of some wrapper classes. Overloading...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
4
by: anaghawalvekar | last post by:
is it true that you cannot overload the assignment operator through a friend function? please give a reason for your answer.
12
by: subramanian100in | last post by:
For a class Test, we write the assignment operator as Test & Test::operator=(const Test & rhs); instead of const Test & Test::operator=(const Test & rhs); that is, the return type of...
9
osfreak
by: osfreak | last post by:
class base { public: base(int data = 0):a(data) {} base(const base& rhs) { a = rhs.a;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.