函数指针,错误无法将参数 1 转换为常量字符

Function Pointers, error cannot convert argument 1 to const char

本文关键字:转换 常量 字符 参数 指针 错误 函数      更新时间:2023-10-16

我迷失了这段代码,我需要做什么

Couple *pcouple1 = new Couple(mary, *pjohn);
Couple couple2(*pjane, mark);

上班?我在夫妇(玛丽和夫妇2(*pjane与以下信息:

*

错误 C2664:"耦合::耦合(常量耦合 &)":无法将参数 1 从"人"转换为"常量字符 *"

*智能感知:构造函数"Couple::Couple"的实例与参数列表匹配 参数类型为:(人,人)

代码是:

班级情侣和人

class Person {
char* name;    
public:
friend class Couple;
friend ostream& operator<<(ostream& str, const Person& os)
{
    str << os.name;
    return str;
};
Person(const char* n)
{
    name = (char*)n;
    cout << "char constructor   " << name << endl;
};
Person(const Person& os)
{
    name = os.name;
    cout << "Person constructor " << name << endl;
};
Person& operator=(const Person& os)
{
    this->name = os.name;
    return *this;   
};
~Person()
{
    free(name);          
    cout << "Class Person Destructor" << endl;
};
char* getName(){
    return name;
    };
};
class Couple {
Person *wife, *husband;
public:
friend ostream& operator<<(ostream& str, const Couple& p)
{
    str << "He " << *p.husband << " She " << *p.wife;
};
Couple::Couple(const char* m, const char* z)
{
    husband = new Person(m);
    wife = new Person(z);
    cout << "Couple1: " << endl;
};
Couple::Couple(const Couple& other)
{
    husband = new Person(*other.husband);
    wife = new Person(*other.wife);
    cout << "Couple2: " << endl;
}
Couple& operator=(const Couple& other)
{
    this->husband = new Person(*other.husband);
    this->wife = new Person(*other.wife);
    return *this;
};

~Couple()
{
    free(husband->name);
    free(husband);
    free(wife->name);
    free(wife);
    cout << "Class Couple Destructor" << endl;
};
};

主要功能:

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;

return 0;
}

有人可以向我推荐一个好的资源/站点/书籍/练习,以更好地理解与此练习类似的函数指针,提前感谢我还在学习。

显然,您的Couple构造函数需要两个char*参数,但您正在尝试使用两个Person对象构造Couple对象。要么引入构造函数,它将接受Person作为参数,要么将给定的Person实例转换为char*