在基 class 中使用派生 class,而派生 class 在基 class 之后声明
Use derived class in base class while derived class is declared after the base class
我有头文件
#ifndef CATEGORY_H
#define CATEGORY_H
#include <functional>
#include <string>
#include <vector>
class Category
{
private:
std::string nameCategory;
std::vector<Rule> setOfRules;
protected:
public:
Category();
Category(std::string nameCategory);
void setIndexBankAccountEntry(unsigned int iBankAccountEntry);
};
class Rule : public Category
{
private:
std::function<void(int)> rule;
protected:
public:
Rule();
Rule(std::function<void(int)> rule);
};
#endif
其中命令 std::vector<Rule> setOfRules;
给出了 Rule
尚未声明的错误。
交换两个类的声明顺序得到
#ifndef CATEGORY_H
#define CATEGORY_H
#include <functional>
#include <string>
#include <vector>
class Rule : public Category
{
private:
std::function<void(int)> rule;
protected:
public:
Rule();
Rule(std::function<void(int)> rule);
};
class Category
{
private:
std::string nameCategory;
std::vector<Rule> setOfRules;
protected:
public:
Category();
Category(std::string nameCategory);
void setIndexBankAccountEntry(unsigned int iBankAccountEntry);
};
#endif
但随后我收到 expected class-name before ‘{’ token
行 class Rule : public Category
.
的错误
我该如何解决这个问题?
您可以转发声明class规则:
#ifndef CATEGORY_H
#define CATEGORY_H
#include <functional>
#include <string>
#include <vector>
class Rule; // <-- here
class Category
{
private:
std::string nameCategory;
std::vector<Rule> setOfRules;
protected:
public:
Category();
Category(std::string nameCategory);
void setIndexBankAccountEntry(unsigned int iBankAccountEntry);
};
class Rule : public Category
{
private:
std::function<void(int)> rule;
protected:
public:
Rule();
Rule(std::function<void(int)> rule);
};
#endif
您可以为 class Rule
使用前向声明(基于第一个示例)。
class Rule;
class Category
{
private:
std::string nameCategory;
std::vector<Rule> setOfRules;
protected:
public:
Category();
Category(std::string nameCategory);
void setIndexBankAccountEntry(unsigned int iBankAccountEntry);
};
// class Rule definition here
勾选here
我有头文件
#ifndef CATEGORY_H
#define CATEGORY_H
#include <functional>
#include <string>
#include <vector>
class Category
{
private:
std::string nameCategory;
std::vector<Rule> setOfRules;
protected:
public:
Category();
Category(std::string nameCategory);
void setIndexBankAccountEntry(unsigned int iBankAccountEntry);
};
class Rule : public Category
{
private:
std::function<void(int)> rule;
protected:
public:
Rule();
Rule(std::function<void(int)> rule);
};
#endif
其中命令 std::vector<Rule> setOfRules;
给出了 Rule
尚未声明的错误。
交换两个类的声明顺序得到
#ifndef CATEGORY_H
#define CATEGORY_H
#include <functional>
#include <string>
#include <vector>
class Rule : public Category
{
private:
std::function<void(int)> rule;
protected:
public:
Rule();
Rule(std::function<void(int)> rule);
};
class Category
{
private:
std::string nameCategory;
std::vector<Rule> setOfRules;
protected:
public:
Category();
Category(std::string nameCategory);
void setIndexBankAccountEntry(unsigned int iBankAccountEntry);
};
#endif
但随后我收到 expected class-name before ‘{’ token
行 class Rule : public Category
.
我该如何解决这个问题?
您可以转发声明class规则:
#ifndef CATEGORY_H
#define CATEGORY_H
#include <functional>
#include <string>
#include <vector>
class Rule; // <-- here
class Category
{
private:
std::string nameCategory;
std::vector<Rule> setOfRules;
protected:
public:
Category();
Category(std::string nameCategory);
void setIndexBankAccountEntry(unsigned int iBankAccountEntry);
};
class Rule : public Category
{
private:
std::function<void(int)> rule;
protected:
public:
Rule();
Rule(std::function<void(int)> rule);
};
#endif
您可以为 class Rule
使用前向声明(基于第一个示例)。
class Rule;
class Category
{
private:
std::string nameCategory;
std::vector<Rule> setOfRules;
protected:
public:
Category();
Category(std::string nameCategory);
void setIndexBankAccountEntry(unsigned int iBankAccountEntry);
};
// class Rule definition here
勾选here