Task.h:
#ifndef Tasks_h
#define Tasks_h
#include "Executors.h"
class Task
{
Executor *current_executor;
};
#endif Tasks_h
Executor.h:
#ifndef Executors_h
#define Executors_h
#include "Tasks.h"
class Executor
{
Task *current_task;
};
#endif Executors_h
main.cpp中:
#include <conio.h>
#include <stdio.h>
#include "Tasks.h"
#include "Executors.h"
int main()
{
Executor ex;
return 0;
}
编译错误:
Error 1 error C2146: syntax error : missing ';' before identifier 'current_task' c:\users\rain\documents\visual studio 2010\projects\text\text\executors.h 8
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\rain\documents\visual studio 2010\projects\text\text\executors.h 8
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\rain\documents\visual studio 2010\projects\text\text\executors.h 8
再次......在C#中我永远不会遇到这样的问题,但我会尽力支持C ++并使用它 不想在一个头文件中编写所有类
答案 0 :(得分:4)
这只是一个例子。可能有指针而不是
如果您可以切换到使用指针,则可以使用前向声明替换标题包含:
#ifndef Executors_h
#define Executors_h
class Task;
class Executor
{
Task *current_task_ptr;
};
#endif Executors_h
答案 1 :(得分:3)
你可以使用前瞻声明:
//#include "Executors.h"
class Executor;
class Task
{
std::shared_ptr<Executor> current_executor;
};
另一种方法是使用pimpl习语:
<。>在.h文件中(没有执行程序标题/转发decl。)class TaskImpl;
class Task
{
public:
Task();
String getExecutorName();
private:
std::unique_ptr<TaskImpl> impl;
friend TaskImpl;
};
和.cpp
#include "Executors.h"
class TaskImpl {
public:
Executor current_executor;
String getExecutorName() { return current_executor.name; }
};
Task::Task() : impl(new TaskImpl()) {
}
String Task::getExecutorName() { return impl->getExecutorName(); }
答案 2 :(得分:1)
我打赌你不知道你在做什么:你有一个任务,其中一部分是执行者,其中一部分是一个任务......
它不是C#或Java中的引用。
要执行您实际尝试的操作,请结合使用前向声明
来使用指针/引用