我有头文件
#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
的错误。
我该如何解决这个问题?
答案 0 :(得分:2)
您可以使用类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
答案 1 :(得分:0)
您可以转发声明类规则:
#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