Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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++ 将ifstream对象传递给函数后,如何读取输入文件?_C++_C++11 - Fatal编程技术网

C++ 将ifstream对象传递给函数后,如何读取输入文件?

C++ 将ifstream对象传递给函数后,如何读取输入文件?,c++,c++11,C++,C++11,正如标题所示,在将ifstream对象传递给类函数后,我遇到了无法读取输入文件的问题。基本上,我尝试使用一个数组实现的heap ADT对数字列表进行排序 int main() { ifstream infile("input.txt"); HeapSort* heap = new HeapSort(20); // creates a heap (array) with size 20 heap->buildHeap(&infile); return 0;

正如标题所示,在将ifstream对象传递给类函数后,我遇到了无法读取输入文件的问题。基本上,我尝试使用一个数组实现的heap ADT对数字列表进行排序

int main() {
   ifstream infile("input.txt");
   HeapSort* heap = new HeapSort(20); // creates a heap (array) with size 20
   heap->buildHeap(&infile);

   return 0;
}

void HeapSort::buildHeap(ifstream* infile) {
    int data;
    while (infile >> data) {cout << data << endl;}
    infile->close();
}
intmain(){
ifstream-infle(“input.txt”);
HeapSort*heap=newheapsort(20);//创建大小为20的堆(数组)
堆->构建堆(&infle);
返回0;
}
void HeapSort::buildHeap(ifstream*infle){
int数据;

当(infle>>data){cout传递一个指向流的指针时,需要取消对它的引用:

while(*infle>>数据)

如果您希望您的代码看起来像您在
main
中所说的那样,那么您将传递一个引用:

heap->buildHeap(infile);
//...
void HeapSort::buildHeap(ifstream& infile) 
{
    int data;
    while (infile >> data) { ... }
    infile.close();
}

您正在传递指向流的指针,因此需要取消对它的引用:

while(*infle>>数据)

如果您希望您的代码看起来像您在
main
中所说的那样,那么您将传递一个引用:

heap->buildHeap(infile);
//...
void HeapSort::buildHeap(ifstream& infile) 
{
    int data;
    while (infile >> data) { ... }
    infile.close();
}

太棒了!非常感谢。所以*符号可以用来引用和取消引用,嗯?我从来不知道。无论如何,再次感谢你!需要
*
符号,因为
操作符>
需要一个对象,而不是左边的指针。要获取对象,必须取消引用指针。@MannyKim,取消引用操作符r(
*
)可用于获取指定地址处的对象(即指针)。用于获取对象的地址(例如
int n=16
)我们使用“同名的代码<代码> >代码> >代码>和N>代码>,C++可以不需要<代码>新< /Cord>创建对象,而不是java。<代码> HeapSort堆(20)使用<代码> < <代码> >在C++中不是这样的一个无辜的操作员,如果你不调用<代码>删除>代码>,它会造成内存泄漏。太棒了!非常感谢。所以*符号可以用来引用和引用,呃?永远不知道。无论如何,再次感谢!r>
需要的是对象,而不是左侧的指针。要获取对象,必须取消对指针的引用。@MannyKim,可以使用取消引用运算符(
*
)在指定地址获取对象(即指针)。要获取对象的地址(例如
int n=16
)我们使用“同名的代码<代码> >代码> >代码>和N>代码>,C++可以不需要<代码>新< /Cord>创建对象,而不是java。<代码> HeapSort堆(20)是您所需要的。使用<代码>新< /C> >在C++中不是一个无辜的操作员,如果您不调用<代码>删除>代码,它将创建内存泄漏。