c++ 关于ofstream,急急在线等,十分着急,就剩这些分数,全给了

来源:百度知道 编辑:UC知道 时间:2024/05/31 20:16:33
"huffman.h"文件如下:
void writecode
(char temp,binarytree* huffmantree,ofstream &outfile);
"huffman.cpp"文件如下:
void writecode(char temp,binarytree* huffmantree,ofstream &outfile)
{
if(temp==huffmantree->ltree->element)
outfile<<0;
else
{
outfile<<1;
writecode(temp,huffmantree->rtree,outfile);
}
}
在调用此函数的地方代码如下:
writecode(temp,huffmantree,&outfile);
编译器报错为:
error C2061: syntax error : identifier 'ofstream'(此指向huffman.h文件的函数声明)
error C2660: 'writecode' : function does not take 3 parameters
执行 cl.exe 时出错.(此指向调用函数的地方)

我已经看了查找了很久,十分着急,就剩这些分数,全给了

huffman.obj - 1 error(s), 0 warning(s)
huffman.cpp文件的头文件包含如下:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "huffman.h"
using namespace std;

#include <iostream>
#include <fstream>
using namespace std;

应该放在"huffman.h"文件
在main()函数的文件中就加这个"huffman.h"头文件

好像在huffman.cpp中加上

#include <fstream>
using namespace std;

这句就行了。

会不会是因为.cpp中没加#include<iostream>

huffman.h文件里加上:
#include <fstream>
using namespace std;
不过最好这么写:
#include <iosfwd>
using std::ofstream;
不要在头文件中暴露整个std命名空间;

huffman.cpp文件里:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "huffman.h"
using namespace std;