C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 27th, 2005, 05:35 PM
br459 br459 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 5 br459 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 50 sec
Reputation Power: 0
Question copyTo function

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;

}




Reply With Quote
  #2  
Old March 1st, 2005, 01:21 AM
af4life af4life is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 4 af4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 18 sec
Reputation Power: 0
Quote:
Originally Posted by br459
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;

}





It appears as though you are trying to copy a class (phone) object into a string array.

Reply With Quote
  #3  
Old March 1st, 2005, 03:42 PM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
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.

Reply With Quote
  #4  
Old March 2nd, 2005, 06:58 PM
br459 br459 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 5 br459 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 50 sec
Reputation Power: 0
Post

Quote:
Originally Posted by BoolBooB
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.


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);


}


Reply With Quote
  #5  
Old March 3rd, 2005, 09:12 AM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
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);



Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > copyTo function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT