C++ 派生 类 调用函数
C++ Derived Classes calling Functions
如果我在 .cpp 文件中有派生的 class,我该如何预先声明?我派生的 class 是百科全书,但是当我想 运行 一个 void 时,我输入 void Encyclopedia::NameofVoid(),但随后出现错误。我尝试了很多东西的组合,但 none 似乎有效。 void Book::Encyclopedia::NameofVoid() 也不起作用,如错误消息所示。
Encuclopedia.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
Encyclopedia.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;
| ^~~~~~~~~~
第一个错误是您要查看的错误:
error: ‘Book::Encyclopedia’ has not been declared
告诉你找不到类型Book::Encyclopedia
。这是真的,你有 Book
和 Encyclopedia
作为不同的 类。 Book::Encyclopedia
将声明为:
class Book {
class Encyclopedia {
因为你的函数 SetEdition
是为 Encyclopedia
定义的,你只需要这个:
void Encyclopedia::SetEdition(string userEdition) {
edition = userEdition;
}
就像你下面的其他功能一样。
如果我在 .cpp 文件中有派生的 class,我该如何预先声明?我派生的 class 是百科全书,但是当我想 运行 一个 void 时,我输入 void Encyclopedia::NameofVoid(),但随后出现错误。我尝试了很多东西的组合,但 none 似乎有效。 void Book::Encyclopedia::NameofVoid() 也不起作用,如错误消息所示。
Encuclopedia.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
Encyclopedia.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;
| ^~~~~~~~~~
第一个错误是您要查看的错误:
error: ‘Book::Encyclopedia’ has not been declared
告诉你找不到类型Book::Encyclopedia
。这是真的,你有 Book
和 Encyclopedia
作为不同的 类。 Book::Encyclopedia
将声明为:
class Book {
class Encyclopedia {
因为你的函数 SetEdition
是为 Encyclopedia
定义的,你只需要这个:
void Encyclopedia::SetEdition(string userEdition) {
edition = userEdition;
}
就像你下面的其他功能一样。