C++派生类调用函数

Posted

技术标签:

【中文标题】C++派生类调用函数【英文标题】:C++ Derived Classes calling Functions 【发布时间】:2021-09-14 02:47:02 【问题描述】:

如果我在 .cpp 文件中有派生类,我该如何事先声明?我的派生类是百科全书,但是当我想运行 void 时,我键入 void Encyclopedia::NameofVoid(),但随后出现错误。我已经尝试了很多东西的组合,但似乎都没有奏效。 void Book::Encyclopedia::NameofVoid() 也不起作用,如错误消息中所示。

百科全书.h

#ifndef ENCYCLOPEDIAH
#define ENCYCLOPEDIAH

#include "Book.h"

#include <string>

using namespace std;

class Encyclopedia : public Book 
   
   public:

   void SetEdition(string userEdition);
   
   string GetEdition;

   
   void SetNumVolumes(int uesrNumVolumes);
   
   int GetNumVolumes;

   void PrintInfo();

   protected:

   string edition;
   string numVolumes;

;

#endif

百科全书.cpp

#include "Encyclopedia.h"
#include <iostream>

// Define functions declared in Encyclopedia.h

void Book::Encyclopedia::SetEdition(string userEdition) 
   edition = userEdition;


string Encyclopedia::GetEdition() 
   return edition;


void Encyclopedia::SetNumVolumes(string userNumVolumes) 
   numVolumes = userNumVolumes;


int Encyclopedia::GetNumVolumes() 
   return numVolumes;


void Enyclopedia::PrintInfo()
   cout << "Book Information: " << endl;
   cout << "   Book Title: " << title << endl;
   cout << "   Author: " << author << endl;
   cout << "   Publisher: " << publisher << endl;
   cout << "   Publication Date: " << publicationDate << endl;
   cout << "   Edition: " << edition << endl;
   cout << "   Number of Volumes: " << numVolumes << endl;

错误:

Encyclopedia.cpp:6:12: error: ‘Book::Encyclopedia’ has not been declared
    6 | void Book::Encyclopedia::SetEdition(string userEdition) 
      |            ^~~~~~~~~~~~
Encyclopedia.cpp: In function ‘void SetEdition(std::string)’:
Encyclopedia.cpp:7:4: error: ‘edition’ was not declared in this scope; did you mean ‘SetEdition’?
    7 |    edition = userEdition;
      |    ^~~~~~~
      |    SetEdition
Encyclopedia.cpp: At global scope:
Encyclopedia.cpp:10:8: error: no declaration matches ‘std::string Encyclopedia::GetEdition()’
   10 | string Encyclopedia::GetEdition() 
      |        ^~~~~~~~~~~~
In file included from Encyclopedia.cpp:1:
Encyclopedia.h:17:11: note: candidate is: ‘std::string Encyclopedia::GetEdition’
   17 |    string GetEdition;
      |           ^~~~~~~~~~
Encyclopedia.h:10:7: note: ‘class Encyclopedia’ defined here
   10 | class Encyclopedia : public Book 
      |       ^~~~~~~~~~~~
Encyclopedia.cpp:14:6: error: no declaration matches ‘void Encyclopedia::SetNumVolumes(std::string)’
   14 | void Encyclopedia::SetNumVolumes(string userNumVolumes) 
      |      ^~~~~~~~~~~~
In file included from Encyclopedia.cpp:1:
Encyclopedia.h:20:9: note: candidate is: ‘void Encyclopedia::SetNumVolumes(int)’
   20 |    void SetNumVolumes(int uesrNumVolumes);
      |         ^~~~~~~~~~~~~
Encyclopedia.h:10:7: note: ‘class Encyclopedia’ defined here
   10 | class Encyclopedia : public Book 
      |       ^~~~~~~~~~~~
Encyclopedia.cpp:18:5: error: no declaration matches ‘int Encyclopedia::GetNumVolumes()’
   18 | int Encyclopedia::GetNumVolumes() 
      |     ^~~~~~~~~~~~
In file included from Encyclopedia.cpp:1:
Encyclopedia.h:22:8: note: candidate is: ‘int Encyclopedia::GetNumVolumes’
   22 |    int GetNumVolumes;
      |        ^~~~~~~~~~~~~
Encyclopedia.h:10:7: note: ‘class Encyclopedia’ defined here
   10 | class Encyclopedia : public Book 
      |       ^~~~~~~~~~~~
Encyclopedia.cpp:22:6: error: ‘Enyclopedia’ has not been declared
   22 | void Enyclopedia::PrintInfo()
      |      ^~~~~~~~~~~
Encyclopedia.cpp: In function ‘void PrintInfo()’:
Encyclopedia.cpp:24:33: error: ‘title’ was not declared in this scope
   24 |    cout << "   Book Title: " << title << endl;
      |                                 ^~~~~
Encyclopedia.cpp:25:29: error: ‘author’ was not declared in this scope; did you mean ‘auto’?
   25 |    cout << "   Author: " << author << endl;
      |                             ^~~~~~
      |                             auto
Encyclopedia.cpp:26:32: error: ‘publisher’ was not declared in this scope
   26 |    cout << "   Publisher: " << publisher << endl;
      |                                ^~~~~~~~~
Encyclopedia.cpp:27:39: error: ‘publicationDate’ was not declared in this scope
   27 |    cout << "   Publication Date: " << publicationDate << endl;
      |                                       ^~~~~~~~~~~~~~~
Encyclopedia.cpp:28:30: error: ‘edition’ was not declared in this scope; did you mean ‘SetEdition’?
   28 |    cout << "   Edition: " << edition << endl;
      |                              ^~~~~~~
      |                              SetEdition
Encyclopedia.cpp:29:40: error: ‘numVolumes’ was not declared in this scope
   29 |    cout << "   Number of Volumes: " << numVolumes << endl;
      |                                        ^~~~~~~~~~

【问题讨论】:

运行 void 是什么意思?你能在这里打印错误信息吗? Book 是你声明的命名空间吗?如果是,您需要定义如下函数: void Book::Encyclopedia::NameofVoid() 请将Encyclopedia.h的内容添加到您的问题中。 【参考方案1】:

第一个错误是您要查看的错误:

错误:'Book::Encyclopedia' 尚未声明

它告诉你它找不到类型Book::Encyclopedia。这是真的,你有 BookEncyclopedia 作为不同的类。 Book::Encyclopedia 将被声明为:

class Book 
    class Encyclopedia 

由于您的函数 SetEdition 是为 Encyclopedia 定义的,因此您只需要这个:

void Encyclopedia::SetEdition(string userEdition) 
   edition = userEdition;

就像你在它下面的其他功能一样。

【讨论】:

@KatieOsadczuk 是的,但你读过他们说的话吗? no declaration matches ‘std::string Encyclopedia::GetEdition() - 因为你还没有声明 std::string GetEdition(); 成员函数,所以你声明了 std::string GetEdition; 成员变量

以上是关于C++派生类调用函数的主要内容,如果未能解决你的问题,请参考以下文章

C++派生类调用函数

C++中派生类的构造函数怎么显式调用基类构造函数?

C++ 为啥我可以从派生类调用基类的私有虚函数?

在 C++ 继承中,当指向基类的指针对象指向派生类时,不调用派生类析构函数

在其派生类C++的构造函数中调用基类的构造函数[重复]

从派生类调用重载函数