why doesnt this work?

D

Developwebsites

#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}
 
M

Moonlit

Hi,

Either use string (preffered way):

string name( "jamesbond" );

Or

strncpy( name, "jamesbond", sizeof name );
or
char *name="jamesbond";

Regards, Ron AF Greve
]
 
V

Victor Bazarov

Moonlit said:
Either use string (preffered way):

string name( "jamesbond" );

Since 'name' is a member, you cannot initialise it that way.
It has to be done in the constructor member initialisation
list. Nonetheless, using string is A VERY GOOD IDEA(tm).
Or

strncpy( name, "jamesbond", sizeof name );

Why are you copying 'sizeof name' bytes? What if there are
more elements in 'name' than 10? You will try accessing memory
beyond the end of the string literal, which causes undefined
behaviour. If you need to copy, (a) make sure you have enough
elements in 'name' and (b) use 'strcpy', which only copies as
many chars as there are in the second argument, plus the
terminating null character.

strcpy(name, "jamesbond");

Of course, if there is not enough elements in 'name', you end
up overrunning memory, which is also undefined behaviour. So,
all things considered, strings are much better alternative,
aren't they?
or
char *name="jamesbond";

It's much better to NEVER declare a pointer to _non-const_ char
and initialise it with a literal. This nonsense is dragging
into C++ from the old C days, and should be avoided. Use

const char * name = "jamesbond";

And, again, if it's a member, it cannot be initialised that way,
blah blah blah

Just my $0.04...
Regards, Ron AF Greve
]
Developwebsites said:
#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}
 
S

Simon Saunders

Why are you copying 'sizeof name' bytes? What if there are more
elements in 'name' than 10? You will try accessing memory beyond the
end of the string literal, which causes undefined behaviour. If you
need to copy, (a) make sure you have enough elements in 'name' and (b)
use 'strcpy', which only copies as many chars as there are in the second
argument, plus the terminating null character.

This is wrong. strncpy(dst,src,n) will stop when either a null character
is found in src or when n bytes have been copied, hence there is no
undefined behaviour here (but note that strncpy doesn't add a terminating
null to dst if strlen(src) >= n).
 
S

someone else

The reason this doesnt work is because you declared name[12] as a char
array, which doesnt have a = operator. as mentioned before, you have 2
options, 1. declare name as a string. 2. use strcpy/strncpy instead of the =
operatior
 

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
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top