Connecting Tech Pros Worldwide Forums | Help | Site Map

Beginner question(s): Passing string into method

James
Guest
 
Posts: n/a
#1: Oct 13 '05
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?



Moonlit
Guest
 
Posts: n/a
#2: Oct 13 '05

re: Beginner question(s): Passing string into method


Hi,



--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

"James" <james@hotmail.com> wrote in message
news:uqy3f.234$CI.124@newsfe2-gui.ntli.net...[color=blue]
> 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);[/color]
------> void SetName(char *n);[color=blue]
>
> private:
> int user_id;[/color]
------> char *name;[color=blue]
> };
> /* END CLASS DEFINITION User */
> #endif /* __USER__ */
>
>
> /* User.cpp */
> #include "User.h"
>
> void User::SetUserID(int u)
> {
> user_id = u;
> }[/color]
//> void User::SetName(char n[])
------>void User::SetName(char *n)
[color=blue]
> {
> name = n;
> }
>
>
>[/color]

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[color=blue]
> P.S. How do you re-initialise an object e.g. in Java it's "new
> ObjectName()", what's the C++ syntax?
>[/color]


Mike Wahler
Guest
 
Posts: n/a
#3: Oct 13 '05

re: Beginner question(s): Passing string into method



"James" <james@hotmail.com> wrote in message
news:uqy3f.234$CI.124@newsfe2-gui.ntli.net...[color=blue]
> 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 */[/color]

#include <string>
[color=blue]
> class User
> {
> public:
> void SetUserID(int u);
> void SetName(char n[]);[/color]

void SetName(const std::string& n);
[color=blue]
>
> private:
> int user_id;
> char name[];[/color]

std::string name;
[color=blue]
> };
> /* END CLASS DEFINITION User */
> #endif /* __USER__ */[/color]

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.
[color=blue]
>
>
> /* User.cpp */
> #include "User.h"[/color]

#include <string>
[color=blue]
>
> void User::SetUserID(int u)
> {
> user_id = u;
> }
> void User::SetName(char n[])[/color]

void User::SetName(const std::string& n)
[color=blue]
> {
> name = n;
> }[/color]

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

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


James
Guest
 
Posts: n/a
#4: Oct 13 '05

re: Beginner question(s): Passing string into method


"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:HFy3f.777$hY6.287@newsread1.news.pas.earthlin k.net...
<snip!>

Thanks guys.


Markus Moll
Guest
 
Posts: n/a
#5: Oct 13 '05

re: Beginner question(s): Passing string into method


Hi

James wrote:
[color=blue]
> 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?[/color]

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;
};
[color=blue]
> P.S. How do you re-initialise an object e.g. in Java it's "new
> ObjectName()", what's the C++ syntax?[/color]

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

Closed Thread


Similar C / C++ bytes