모든 클래스에 대해 'class name' does not name a type
오류가 발생합니다. 순환 의존성이 있다고 의심하지만 각 클래스가 다음 클래스의 함수에 액세스해야하므로 해결 방법이 없습니다. 다음은 내 클래스는 다음과 같습니다"형식 이름을 지정하지 않습니다"오류를 해결하는 방법
Container.h :
#ifndef CONTAINER_H
#define CONTAINER_H
#include "Factory.h"
class Container
{
public:
Container()
{
array = new int[10];
for (int i = 0; i < 10; ++i) {
array[i] = i;
}
}
Iterator* createIterator()
{
Factory fac;
return fac.factoryMethod();
}
friend class Iterator;
private:
int* array;
};
#endif //CONTAINER_H
Factory.h :
#ifndef FACTORY_H
#define FACTORY_H
#include "Iterator.h";
class Factory
{
Iterator* factoryMethod(Container* con)
{
return new Iterator(con);
}
};
#endif //FACTORY_H
Iterator.h :
#ifndef ITERATOR_H
#define ITERATOR_H
#include "Container.h"
class Iterator
{
public:
Iterator(Container* con)
{
this->con =con;
}
int getFromIndex(int i)
{
return con->array[i];
}
private:
Container* con;
};
#endif //ITERATOR_H
MAIN.CPP :
#include <iostream>
using namespace std;
#include "Container.h"
#include "Iterator.h"
int main() {
Container con;
Iterator* it = con.createIterator();
cout<<it->getFromIndex(2)<<endl;
return 0;
}
도움을 주셔서 감사합니다.
.cpp 파일에 함수 본문을 넣습니다. – Dan
@ 단 이걸 시도했지만 데모 목적으로 만 인라인으로 넣으십시오. – Keagansed