C++ 派生类调用函数

凯蒂·奥萨德丘克

如果我在 .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;
      |                                        ^~~~~~~~~~
神奇的狐狸先生

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

错误:“图书::百科全书”尚未声明

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

class Book {
    class Encyclopedia {

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未调用派生类的c ++ / cli静态构造函数

C ++多态性:派生类调用基类虚拟函数,而不是重写的派生类

在派生类 C++ 的构造函数中调用基类的构造函数

C ++派生类构造函数调用基类构造函数错误

C#-在派生类中调用基类和类构造函数

C ++继承:使用派生类的实例调用基类的虚函数

如何在C ++中从基类构造函数调用派生类方法?

C# 可以让基类中的泛型函数知道调用派生类的类型吗?

C ++-派生类构造函数的行为

在C ++中从派生类的析构函数调用虚函数

C ++:用指向派生类的指针进行的虚函数调用是否仍具有vlookup

有关在派生类的基础上实现的C ++调用虚拟函数的问题

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

C ++继承:指向基类的派生类指针调用派生类方法

即使它们是私有的,基类的好友函数如何调用派生类的重写虚拟函数?(C ++)

如何调用在C ++中具有抽象基类和派生类的定义的基类的虚函数定义?

C ++如何从多个派生类调用基类方法?

C ++:在派生类成员上调用基类方法

C ++派生类

派生类C ++

C++覆盖派生类成员函数返回派生类数组

当在C ++中获得指向基类的指针时,为什么不为派生类对象调用重载函数?

在 C++ 中使用模板调用派生类中父类的构造函数的正确语法是什么?

C++11:我可以显式调用基类析构函数来销毁派生类吗?

C#如何从派生类调用接口方法

在 C++ 中,如何使用向量调用派生类?

派生类中的C ++纯虚函数实现

在派生类中重写C ++模板函数

C ++ 11在派生类中删除的析构函数