我是C ++编程的新手,我正在尝试执行以下代码,但是它 显示错误
没有匹配函数可调用“ Flower :: Flower()” Rose(字符串n = “没有花”,字符串c =“红色”):color(c){}
即使我在Flower类中给了参数构造函数,它仍然说没有匹配的函数调用。
#include <iostream>
#include <string>
using namespace std;
class Flower
{
public:
string name;
Flower (string n):name (n)
{
}
void getFlowerName ()
{
cout << name << " " << "is" << " ";
}
};
class Rose:private Flower
{ // Inherit Flower as private
public:
string color;
/* Initialize name and color data members */
Rose (string n = "No flower", string c = "Red"):color (c)
{
}
void getFlowerName (Rose & r)
{
cout << r.color << endl;
}
// using Flower::getFlowerName ;// Allow call to getFlowerName() method in the base class
};
class Rose:private Flower
{ // Inherit Flower as private
public:
string color;
/* Initialize name and color data members */
Rose (string n = "No flower", string c = "Red"):color (c)
{
}
void getFlowerName (Rose & r)
{
cout << r.color << endl;
}
using Flower::getFlowerName; // Allow call to getFlowerName() method in
the base class
};
答案 0 :(得分:2)
派生类应调用基类:
class Rose : private Flower
{
public:
std::string color;
Rose (const std::string& n = "No flower",
const std::string& c = "Red") : Flower(n), color(c)
{
}
// ...
};
目前,您隐式调用默认的Flower
的构造函数,但它不存在。
答案 1 :(得分:-1)
创建Flower::Flower()
默认构造函数。
Flower(){ name = NULL; }