| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have a class phone with a member function
void copyTo member function. I am passing a phone number with area code to the function. My thought was to make a workspace and use the strncpy command to copy the phone number as a string. The error I get tells me strncpy can't convert from phone to char *. I am trying to copy the invoking instance and use it in my set function. Help void phone::copy ( phone & r) const { char s[13]; strncpy (r,s,13); s[12] = 0; } |
|
#2
|
|||
|
|||
|
Quote:
It appears as though you are trying to copy a class (phone) object into a string array. |
|
#3
|
|||
|
|||
|
What exactly are you trying to do, make a copy of a Phone object? The code as writen is trying to copy a character array (that has not been initialized) to a reference to a phone object, then it sets the last position in the array to the null character. Is this what you are trying to do? Also, the array will destroyed as soon as the function exits.
|
|
#4
|
|||
|
|||
|
Quote:
I am trying to pass a phone number as input into a character array and then use this array as an input parameter for my setPhone function. It is a member function of my class phone. I am probably not explaining this very well. My phone class member function void setPhone(constchar []); void phone::setPhone( constchar sn[]) { char s[13]; strncpy(s,sn,13); s[12] = '\0'; if ( strlen(s) == 12) // need to remove the SEPARATOR { strncpy(s+3,s+4,3); // copy from cell 4 for 3 bytes to cell 3 strncpy(s+6,s+7,4); // copy from cell 7 for 4 bytes to cell 6 s[13] = '\0'; } int work = atoi(s); // convert from string to integer setPhone(work); } |
|
#5
|
|||
|
|||
|
I'm still not sure what you are trying to do. The character array:
char s[13]; that you define inside of setPhone() is a dynamic (temporary) variable. As soon as you exit setPhone() it will be destroyed. Why set it, just to exit the function and have it destroyed? If you want that value to be permanent (and I assume you do) it needs to be a member of your phone class: class phone { private: char s[13]; public: void setPhone(); ... }; Then inside of setPhone() you can access it as "s" as if it were local to the function, or also as "this->s". What are you trying to do here? int work = atoi(s); // convert from string to integer setPhone(work); Unless you have a version of setPhone() that accepts an int as input this wont work. If you want the ability to call set phone with either an integer (not sure why) or a char[], you'll need two versions of setPhone(): setPhone(const char[] s); setPhone(const int i); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > copyTo function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|