我想要一个(int,fstream *)的映射,并使用一些函数对其进行修改。我可以在main中轻松修改它,但是如果我想通过发送指向fstream的指针来使用它,则会出现此编译器错误:错误C2440:'=':无法从'std :: fstream'转换为'std :: basic_fstream *'

map<int, fstream*> m;
void func(fstream* f){
m[0] = *f; //compile error
}

int main( int argc, const char* argv[] )
{
fstream f("hi.txt");
func(&f); //error
m[0] = &f;   //work fine
f.close();
system("pause");
}


我该如何更改?

最佳答案

不要在函数内部取消引用指针。

使用

void func(fstream* f){
  m[0] = f; //no more compile errors
}

关于c++ - C++中fstream指针的映射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15920984/

10-13 08:15