Beginner question(s): Passing string into method

J

James

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?
 
M

Moonlit

Hi,



--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

James said:
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
 
M

Mike Wahler

James said:
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 said:
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 said:
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
 
M

Markus Moll

Hi
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top