我开始学习C++(来自Java),所以请耐心等待.
我似乎无法接受我的方法声明来接受我所制作的课程.
"上下文"尚未宣布
我想我不理解一个基本概念,但我不知道是什么.
Expression.h
#include "Context.h"
class Expression {
public:
void interpret(Context *); // This line has the error
Expression();
virtual ~Expression();
};
Run Code Online (Sandbox Code Playgroud)
Context.h
#include <stack>
#include <vector>
#include "Expression.h"
class Context {
private:
std::stack<Expression*,std::vector<Expression*> > theStack;
public:
Context();
virtual ~Context();
};
Run Code Online (Sandbox Code Playgroud)
你必须转发声明Expression,Context反之亦然(或两者),否则你有一个循环依赖.例如,
Expression.h:
class Context; // no include, we only have Context*.
class Expression {
public:
void interpret(Context *); // This line has the error
Expression();
virtual ~Expression();
};
Run Code Online (Sandbox Code Playgroud)
Context.h:
#include <stack>
#include <vector>
class Expression; // No include, we only have Expression*
class Context {
private:
std::stack<Expression*,std::vector<Expression*> > theStack;
public:
Context();
virtual ~Context();
};
Run Code Online (Sandbox Code Playgroud)
您可以执行前向声明,因为不需要完整的类定义,因为在每种情况下您只是指向其他类的指针.您可能需要实现文件中的包含(即#include "Context.h"in Expression.cpp和#include Expression.hin Context.cpp).
最后,请记住在头文件中加入包含警戒.
| 归档时间: |
|
| 查看次数: |
119 次 |
| 最近记录: |