gpt4 book ai didi

c++ - 简单的 C++ 继承示例,有什么问题?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:52 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Function with same name but different signature in derived class

我正在尝试编译它,但我无法弄清楚代码有什么问题。我在 Xcode g++ 4.2.1 版中使用 MacOSX Snow Leopard。有人可以告诉我问题是什么吗?我认为这应该编译。这不是我的功课,我是一名开发人员……至少我认为我是,直到我被这个难住了。我收到以下错误消息:

error: no matching function for call to ‘Child::func(std::string&)’
note: candidates are: virtual void Child::func()

代码如下:

#include <string>

using namespace std;

class Parent
{
public:
Parent(){}
virtual ~Parent(){}
void set(string s){this->str = s;}
virtual void func(){cout << "Parent::func(" << this->str << ")" << endl;}
virtual void func(string& s){this->str = s; this->func();}
protected:
string str;
};

class Child : public Parent
{
public:
Child():Parent(){}
virtual ~Child(){}
virtual void func(){cout << "Child::func(" << this->str << ")" << endl;}
};

class GrandChild : public Child
{
public:
GrandChild():Child(){}
virtual ~GrandChild(){}
virtual void func(){cout << "GrandChild::func(" << this->str << ")" << endl;}
};

int main(int argc, char* argv[])
{
string a = "a";
string b = "b";
Child o;
o.set(a);
o.func();
o.func(b);
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com