C++ 如何解决“未命名类型”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39723267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve "Does not name a type" error
提问by Keagansed
I am getting the following error:
'class name' does not name a type
for all of my classes.
I suspect it may be a circular dependency but I have no clue how to solve it as each class requires access to a function from the next. Below are my classes:
我收到以下错误:
'class name' does not name a type
对于我的所有课程。我怀疑这可能是循环依赖,但我不知道如何解决它,因为每个类都需要从下一个类访问一个函数。下面是我的课:
Container.h:
容器.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:
工厂.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:
迭代器.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:
主.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;
}
Thank you in advance for any help.
预先感谢您的任何帮助。
回答by Angew is no longer proud of SO
It is indeed a circular dependency between your headers. Container.h
includes Factory.h
, which includes Iterator.h
, which includes Container.h
.
这确实是您的标题之间的循环依赖。Container.h
包括Factory.h
,其中包括Iterator.h
,其中包括Container.h
。
The solution is to move the implementations of member functions from header files into source files. That way, header files will only need declarations, not definitions, of the classes, which you can easily put directly in the "consuming" header files:
解决方案是将成员函数的实现从头文件移动到源文件中。这样,头文件只需要类的声明,而不是定义,您可以轻松地将它们直接放入“消费”头文件中:
class Iterator;
class Container
{
public:
Container();
Iterator* createIterator();
friend class Iterator;
private:
int* array;
};
Then, in an appropriate source file (such as Container.cpp
), implement the member functions and include any headers you need:
然后,在适当的源文件(例如Container.cpp
)中,实现成员函数并包含您需要的任何头文件:
Container.cpp
容器.cpp
#include "Container.h"
#include "Factory.h"
Container::Container() : array(new int[10])
{
for (int i = 0; i < 10; ++i) {
array[i] = i;
}
}
Iterator* Container::createIterator()
{
Factory fac;
return fac.factoryMethod();
}
(Dtto for Factory
and Iterator
, of course).
(Dtto for Factory
and Iterator
,当然)。
Don't forget to link all the source files together when building your final binary.
在构建最终二进制文件时,不要忘记将所有源文件链接在一起。
回答by Ivan Rubinson
As Dan said, put the function bodies in .cpp files (different translation units).
正如丹所说,将函数体放在 .cpp 文件(不同的翻译单元)中。
Also, if you're only using pointers to a type, you don't have to #include
it. Just do forward-declaration.
此外,如果您只使用指向类型的指针,则不必使用#include
它。只需做前向声明。