定义之前的结构声明
Struct declaration before definition
我的C++代码有一个小问题。
class Command {
public:
virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
virtual void help(int option) = 0;
};
struct CommandDesc
{
std::string name;
std::string description;
Command* comobj; //Issue is here
};
如果我在类之前定义结构,我将无法定义成员
Command* comobj;
如果我在类之后定义,我将无法将结构的实例传递给具有的方法
virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
你能提出什么建议?有没有什么方法可以先声明结构,而不是单独定义它?
好的,如果我在上课前定义结构,我将无法定义成员
Command* comobj;
由于comobj是一个指针,因此可以转发声明Command
来解决此问题。
你可以这样做:
class Command;
struct CommandDesc
{
std::string name;
std::string description;
Command* comobj;
};
class Command {
public:
virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
virtual void help(int option) = 0;
};
是-简单-前向声明
只需放入
class Command;
struct CommandDesc
{
....
}
class Command {
As before
};
相关文章:
- 为什么 C++ 中的以下结构声明会导致退出 127?
- 在.cpp文件之间传递结构声明,而不使用全局头文件
- 结构声明中的 if 语句C++
- 这个嵌套结构声明是什么意思?
- 在结构声明的分号之前声明的结构变量是否为全局变量
- 为什么结构声明在C++中违反了 ODR
- 错误:多个文件中的结构声明冲突
- 为什么调用定义的构造函数会导致结构声明中出现错误,我该如何修复它
- C++数据结构声明
- 相互依赖的结构声明
- 结构声明的数组
- 结构声明中的":"是什么意思
- 面向C++的main.cpp中的可视化结构声明问题
- 当某些结构字段被省略或与结构声明中的顺序不同时,如何实现正确的解析?
- 编译器在转发结构声明上出错
- C++生成器 XE4 中的结构声明
- 我无法理解c ++中的结构声明
- 嵌套结构声明以及如何访问它们
- 将包含单个数组的结构声明为packed有什么缺点吗
- 定义之前的结构声明