【问题标题】:error: no ‘’ member function declared in class ''错误:没有''在类''中声明的成员函数
【发布时间】:2014-01-21 13:36:03
【问题描述】:

我正在尝试创建一个包含虚函数的类,我想在两个子类中继承它。

我知道有些人已经问过这个问题(例如herethere),但我无法理解答案。

所以我做了一个我正在尝试的简化示例代码:

//Mother .h file 

#ifndef _MOTHER_H_
#define _MOTHER_H_
#include <iostream>

class Mother
{
 protected :
   std::string _name;
 public:
   Mother(std::string name);
   ~Mother();
   virtual std::string getName() = 0; 
};

#endif

//Mother .cpp file

#include "Mother.h"

Mother::Mother(std::string name)
{
  this->_name = name; 
}

Mother::~Mother()
{
}


//Child.h file

#ifndef _CHILD_H_
#define _CHILD_H_
#include "Mother.h"

class Child : public Mother
{
 private : 
   std::string _name;
 public:
   Child(std::string name);
   ~Child();
};

#endif


//Child .cpp file

#include "Mother.h"
#include "Child.h"

Child::Child(std::string name) : Mother(name)
{
  this->_name = name;
}

Child::~Child()
{
}

std::string Mother::getName()
{
  return this->_name;
}

这是我的 main.cpp 文件:

//Main.cpp file
#include "Child.h"

int main()
{
   Child l("lol");

   std::cout << l.getName() << std::endl;

   Mother& f = l;

   std::cout << f.getName() << std::endl;

   return 0;
 }

编译器是这样说的: (使用 g++ *.cpp -W -Wall -Wextra -Werror 编译)

main.cpp: In function ‘int main()’:
main.cpp:5:9: error: cannot declare variable ‘l’ to be of abstract type‘Child’
In file included from main.cpp:1:0:
Child.h:8:7: note:   because the following virtual functions are pure within ‘Child’:
In file included from Child.h:6:0,
             from main.cpp:1:
Mother.h:14:23: note:   virtual std::string Mother::getName()

我做错了什么?

(对不起,如果我犯了一些英语错误,我不是母语人士)。

【问题讨论】:

  • 您没有在子类中覆盖纯虚函数 getName()。
  • 你需要在你的子类中实现getName()函数。
  • 为什么 getName() 是纯的?
  • 在 OOP 世界中,孩子并没有真正从母亲那里继承。
  • 包含守卫的名称使用保留标识符,stackoverflow.com/questions/228783/…

标签: c++ class inheritance virtual


【解决方案1】:

Mother 的声明中你有:

virtual std::string getName() = 0; 

这不仅仅是一个virtual,而是一个 virtualvirtual virtual 之间的区别在于纯变体必须在派生类中实现覆盖,即使您在基类。例如:

class Foo
{
public:
  virtual void DoIt() = 0 {};  // pure virtual.  Must be overridden in the derived class even though there is an implementation here
};

class Bar : public Foo
{
public:
  void DoIt(); // override of base
};

void Bar::DoIt()
{
  // implementation of override
}

你不能用未实现的纯virtual 方法来实例化一个类。如果你尝试,你会得到一个编译器错误:

int main()
{
  Foo f;  // ERROR
  Bar b;  // OK
}

这正是你试图做的。你声明 getName()pure virtual in Mother,但您没有在 Child 中覆盖它。然后你尝试实例化一个Child

int main()
{
   Child l("lol");

导致编译器错误。

要解决此问题,请在 Child 类中覆盖 getName()

【讨论】:

  • std::string Child::getName() { return Mother::getName(); }?
  • 非常感谢!现在我明白了我的错误! :) 我仍然不明白它背后的逻辑,但现在我现在如何使用它。
  • @Joker_vD:那不会编译。 “母亲”中没有实现
  • @JohnDibling 有,一直向下滚动问题中的代码。
  • @Joker_vD:啊,是的,我错过了。
【解决方案2】:

class child 应该重写 getName() 方法,因为它是在 pure virtual 中定义的 class mother

对我来说似乎是错字......因为std::string Mother::getName() 定义在child.cpp..

std::string Child::getName()
{
  return this->_name;
}

【讨论】:

  • 嗯...这行得通...但是如果我已经在 Mother 类中定义了 Child 中的 Function 有什么意义呢?
  • 事实上这是错误的..在类mother中应该没有函数名getName......因为它是纯虚拟的(body =0)。在此示例中,必须只有一个 getName ..并且必须在 Child!
  • @Digital_Reality 好吧,在声明它的类中定义纯虚函数并不违反标准。它将允许您将Child::getName 实现为return Mother::getName()
  • @Joker_vD 是的,但我不太确定,如果定义一个纯虚函数是否有意义!!
【解决方案3】:

从 OOP 的角度来看,基类 Mother 中的纯虚函数没有意义,它是所有子类的共同特征,因此可以使用相同的函数。不需要覆盖它。

struct Person
{
  Person(std::string name) : _name(name) {}
  std::string _name;
  std::string getName() {return _name; }
};

struct Mother : Human
{
   Mother(std::string name) : Person(name) {}
};

struct Child : Human
{
   Child(std::string name) : Person(name) {}
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多