Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;远期申报问题_C++_Forward Declaration - Fatal编程技术网

C++ C++;远期申报问题

C++ C++;远期申报问题,c++,forward-declaration,C++,Forward Declaration,我有一个头文件,它有一些转发声明,但是当我在实现文件中包含头文件时,它会在前面的转发声明的include之后被包含,这会导致类似这样的错误 error: using typedef-name ‘std::ifstream’ after ‘class’ /usr/include/c++/4.2.1/iosfwd:145: error: ‘std::ifstream’ has a previous declaration. class ifstream; class A { ifstre

我有一个头文件,它有一些转发声明,但是当我在实现文件中包含头文件时,它会在前面的转发声明的include之后被包含,这会导致类似这样的错误

error: using typedef-name ‘std::ifstream’ after ‘class’
/usr/include/c++/4.2.1/iosfwd:145: error: ‘std::ifstream’ has a previous declaration.

class ifstream;

class A
{
    ifstream *inStream;
}
// End of A.h

#include <ifstream>
using std::ifstream;

#include "A.h"

// etc
错误:在“类”之后使用typedef名称“std::ifstream”
/usr/include/c++/4.2.1/iosfwd:145:错误:“std::ifstream”前面有一个声明。
类ifstream;
甲级
{
ifstream*流内;
}
//A.h.的结尾
#包括
使用std::ifstream;
#包括“A.h”
//等
围绕这个问题工作的标准是什么


提前谢谢。

您是如何转发的?问题可能在于
std::ifstream
是一个类而不是一个类。

不要向前声明std:ifstream-只需导入

ifstream是一个typedef


有关更多详细信息,请参见此处:

如果要向前声明某些iostreams类,只需包含
。该标题为这些类提供了转发声明。

您实际上有两个问题

第一个是,在C++中,向前声明一个Type是相当困难的,正如Kirill已经指出的。


第二个是
ifstream
basic\u ifstream
的特定模板实例化的类型定义——为了使编译器能够扩展模板,它必须已经定义了模板的主体,这意味着您不能向前声明实例化的模板。

您在
A
的类定义后缺少分号,请执行以下操作(如果您在自己的模板之前已经包含std头文件,则无需再向前声明):

在您的cpp文件中:

#include <iostream>
#include "a.h"    
检查这个

namespace std
{
    template<class E, class T> class basic_ifstream;
    template<class E> struct char_traits;
    typedef basic_ifstream<char, char_traits<char> > ifstream;
}
名称空间std
{
模板类基本流;
模板结构特征;
typedef basic_ifstream ifstream;
}

你能发布一些最小的代码吗?你忘了一个
A类定义之后
首先。。。
namespace std
{
    template<class E, class T> class basic_ifstream;
    template<class E> struct char_traits;
    typedef basic_ifstream<char, char_traits<char> > ifstream;
}