This is a lab exercise for homework due tomorrow, and I'm trying to figure out how to code Person.cpp. Person.h and Main.cpp are from my teacher and need no modification. Any help is very apperciated. :)
>c:\users\jason\documents\visual studio 2010\projects\labexercise07\labexercise07\main.cpp(31): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Person' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
In line 23 of Person.h you are given a string*. You need to allocate some memory for it in the constructor using new string;. Don't forget to delete it in the destructor.
So how do I use new string? I think in person.h file it's a pointer pointing to nothing. So how would I make it modify the variable string name that's not a pointer?? could anyone write an example with my code, it would be greatly apperciated
1 2 3 4
Person::Person()
{
name = new string;
}
Like that?
But now I can't figure out what's wrong with
Main.cpp
1 2 3
// test insertion operator
cout << *p1Ptr << endl; // empty string will print
cout << *p2Ptr << endl; // empty string will also print
Your constructor is now correct. Don't forget to delete the string in your destructor.
Your insertion operator looks fine to me.
You can't do your copy constructor like that. You will end up with both copies pointing at the same string. You need to assign a new string with new and copy.