Printing the string pointer displays the memory location, not the value
Sep 4, 2012 at 8:24am UTC
When I print the string pointer the output is, what I guess, the memory location: 0xbfc010.
How can I get it to print the value?
1 2 3
void printPerson(Person *p) {
cout << "Name: " << p->getName() << ", age: " << p->getAge() << endl;
}
The Person class looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
class Person {
private :
string *name;
int age;
public :
Person(string *n, int age) {
this ->name = n;
this ->age = age;
}
~Person() {
delete name;
}
void setName(string *name) {
this ->name = name;
}
string *getName() {
return this ->name;
}
void setAge(int age) {
this ->age = age;
}
int getAge() {
return this ->age;
}
};
Last edited on Sep 4, 2012 at 8:24am UTC
Sep 4, 2012 at 9:10am UTC
You have to dereference the pointer using operator* to get what the pointer points to.
cout << "Name: " << * p->getName() << ", age: " << p->getAge() << endl;
Topic archived. No new replies allowed.