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

Beginner question(s): Passing string into method

How do you pass a string into a function and set the contents of an internal
string field to be the contents of the string passed into the Set method?

My attempt at doing this below doesn't work and produces errors such as
"class 'User' has an illegal zero-sized array" and "'=' : cannot convert
from 'char[]' to char[]'".

/* User.h */
class User
{
public:
void SetUserID(int u);
void SetName(char n[]);

private:
int user_id;
char name[];
};
/* END CLASS DEFINITION User */
#endif /* __USER__ */
/* User.cpp */
#include "User.h"

void User::SetUserID(int u)
{
user_id = u;
}
void User::SetName(char n[])
{
name = n;
}

P.S. How do you re-initialise an object e.g. in Java it's "new
ObjectName()", what's the C++ syntax?
Oct 13 '05 #1
4 3427
Hi,

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"James" <ja***@hotmail.com> wrote in message
news:uq**************@newsfe2-gui.ntli.net...
How do you pass a string into a function and set the contents of an
internal string field to be the contents of the string passed into the Set
method?

My attempt at doing this below doesn't work and produces errors such as
"class 'User' has an illegal zero-sized array" and "'=' : cannot convert
from 'char[]' to char[]'".

/* User.h */
class User
{
public:
void SetUserID(int u); ------> void SetName(char *n);
private:
int user_id; ------> char *name; };
/* END CLASS DEFINITION User */
#endif /* __USER__ */
/* User.cpp */
#include "User.h"

void User::SetUserID(int u)
{
user_id = u;
} //> void User::SetName(char n[])
------>void User::SetName(char *n)
{
name = n;
}

Just pass a pointer to char. However be careful with above code since 'name'
now points to the same memory as the pointer you passed in your routine. You
are more likely to want to do the following:

void User::SetName(char *n)
{
// Delete any possibly previously allocated name or do nothing if name is
0
delete[] name;
name = new char[ strlen( n ) + 1 ];
strcpy( name, n );
// DON' T forget to add delete[] name in the destructor and initialize
name with 0 in the constructor
}

Or what I prefer just use the STL string class

#include <string>
using namespace std;

void User::SetName( const string& Name )
{
this->name = Name;
}

Regards, Ron AF Greve P.S. How do you re-initialise an object e.g. in Java it's "new
ObjectName()", what's the C++ syntax?

Oct 13 '05 #2

"James" <ja***@hotmail.com> wrote in message
news:uq**************@newsfe2-gui.ntli.net...
How do you pass a string into a function and set the contents of an
internal string field to be the contents of the string passed into the Set
method?

My attempt at doing this below doesn't work and produces errors such as
"class 'User' has an illegal zero-sized array" and "'=' : cannot convert
from 'char[]' to char[]'".

/* User.h */
#include <string>
class User
{
public:
void SetUserID(int u);
void SetName(char n[]);
void SetName(const std::string& n);

private:
int user_id;
char name[];
std::string name;
};
/* END CLASS DEFINITION User */
#endif /* __USER__ */
I'll assume this '#endif' matches an '#ifdef'
or '#ifndef' which you didn't include here.
But note that identifiers beginning with
two underscores are reserved to the implementation,
you should choose a different name.


/* User.cpp */
#include "User.h"
#include <string>

void User::SetUserID(int u)
{
user_id = u;
}
void User::SetName(char n[])
void User::SetName(const std::string& n)
{
name = n;
}

P.S. How do you re-initialise an object e.g. in Java it's "new
ObjectName()", what's the C++ syntax?


In C++ 're-initialize' has no meaning. An object can
be initialized with a value when it is created. After
that, its value can only be changed via assignment.
Initialization and assignment are not the same thing.

int i = 42; /* create and initialize 'i' */
i = 0; /* change value of 'i' */

-Mike
Oct 13 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:HF***************@newsread1.news.pas.earthlin k.net...
<snip!>

Thanks guys.
Oct 13 '05 #4
Hi

James wrote:
How do you pass a string into a function and set the contents of an
internal string field to be the contents of the string passed into the Set
method?
Just use a std::string.

#include <string>

class User
{
public:
void SetName(const std::string& n) { name = n; }
// or maybe (std::string n) for now...

private:
std::string name;
};
P.S. How do you re-initialise an object e.g. in Java it's "new
ObjectName()", what's the C++ syntax?


Pardon? AFAIK you cannot re-initialize any object in Java, as little as you
can in C++. In Java as well as in C++, "new ClassName()" creates a new
object, just as the name says.

Markus

Oct 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I am trying to work through a PHP book that I bought a year or so ago, "Essential PHP" by Christopher Cosentino. In the beginning of the book, after the Hello, World .php file, you get to a...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
16
by: dev | last post by:
Hello, How can the following code be converted to VB.Net? I am mostly interested in how to handle the StrPtr calls in the function. Private Declare Function PathRelativePathToW Lib...
1
by: hl | last post by:
Hi, I'm a beginner and need a little help with getting data back from a web service. I am using VB.Net and have added a web reference to a Wsdl that was provided to me. My reference.vb file...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
6
by: Chris | last post by:
Hi all, I've recently discovered the joys of tag :) I have a ListView which is populated with strings from a List<>, and each ListView item is linked to the relevant object in List<> using Tag....
5
by: Jacky Luk | last post by:
import java.awt.Graphics; public class printtest extends java.applet.Applet { public void init() { } public void paint (Graphics g) {
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.