【发布时间】:2018-03-30 03:22:57
【问题描述】:
尝试在头文件中使用 ifstream 时,我不断收到错误消息。他们说:
FloatList.h:14:15: error: 'ifstream' has not been declared
void getList(ifstream&);
FloatList.cpp:16:6: error: prototype for 'void FloatList::getList(std::ifstream&)'
FloatList.h:14:7: error: candidate is: void FloatList::getList(int&)
void getList(ifstream&);
这是 my.h 文件中的问题部分:
public:
FloatList(); // constructor that sets length to 0.
~FloatList(); // destructor
void getList(ifstream&); // Member function that gets data from a file
void printList() const; // Member function that prints data from that
// file to the screen.
};
#endif
这是我的成员函数的实现文件:
#include "FloatList.h"
#include <iostream>
#include <fstream>
using namespace std;
// Fill in the entire code for the getList function
// The getList function reads the data values from a data file
// into the values array of the class FloatList
void FloatList::getList(ifstream& file)
{
for(int i = 0; i < MAX_LENGTH; i++)
{
if(file >> values[i])
length++;
}
}
这和我在头文件中使用 ifstream 的方式有关系吗?
【问题讨论】:
-
你的头文件有
#includes吗? -
不,但我在头文件中使用了 std::ifstream ,现在一切正常,谢谢。