#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class Person {
char* name;
public:
explicit Person(const char* n)
{
name = new char[strlen(name)+1 ];
strcpy(name, n);
}
Person(const Person& os)
{
name = new char[strlen(os.name)+1 ];
strcpy(name, os.name);
}
Person& operator=(const Person& os)
{
if (&os != this)
{
delete[] name;
name = new char[strlen(os.name)+1 ];
strcpy(name, os.name);
}
return *this;
}
friend class Couple;
friend std::ostream& operator<<(std::ostream& str,const Person& os)
{
str << os.name;
return str;
}
~Person()
{
cout << "Delete " << name;
delete[] name;
}
};
class Couple {
Person *wife, *husband;
public:
Couple(const Person& she, const Person& he):wife(she),husband(he){}
Couple(const Couple& other):wife(p.wife),husband(p.husband){}
Couple& operator=(const Couple& other)
{
husband = p.husband;
wife = p.wife;
return*this;
}
friend std::ostream& operator<<(std::ostream& str,const Couple& p)
{
str << p.husband << " " << p.wife;
return str;
}
~Couple();
};
int main(void) {
Person *pjohn = new Person("John"),
*pjane = new Person("Jane");
Person mary("Mary"), mark("Mark");
Couple *pcouple1 = new Couple(mary, *pjohn);
Couple couple2(*pjane, mark);
delete pjohn;
delete pjane;
cout << *pcouple1 << endl;
cout << couple2 << endl;
couple2 = *pcouple1;
delete pcouple1;
cout << couple2 << endl;
}
Wywala mi błędy :
||=== Build: Debug in Zadanie10 (compiler: GNU GCC Compiler) ===|
||In constructor 'Couple::Couple(const Person&, const Person&)':|
|55|error: cannot convert 'const Person' to 'Person*' in initialization|
|55|error: cannot convert 'const Person' to 'Person*' in initialization|
|56|error: 'p' was not declared in this scope|
||In member function 'Couple& Couple::operator=(const Couple&)':|
|59|error: 'p' was not declared in this scope|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Jak by mi ktoś podpowiedział jak to naprawić to by super było.